Re: var vs def
scalanewbie <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
Thank you Martin and Oliver,
This is now clear to me (I think), correct me if am wrong below:
I created another small example, but this time with no variable in var
declaration. Here is the example, and I start with initialising a variable
i with 0
scala> var i = 0
i: Int = 0
scala> var greatWorld3:Boolean = if (i>10) true else { println("Print i:"+i)
| i += 1
| greatWorld3 }
Print i:0
greatWorld3: Boolean = false
scala> i
res44: Int = 1
scala> greatWorld3
res45: Boolean = false
Now, I start with a variable i set to 0; and declare a function (I think it
is still a Function/Method; as when I did not provide *:Boolean* return
type it complained as being recursive without any return type; do correct
me if I am getting this wrong).
Given that the right side of '=' sign is only evaluated once, the statement
is run once therefore the i ends up with 1 value at the end of it. And for
the same reason, the *greatWorld3* variable/function (ummm... I am still
confused here if I defined a function or variable...) has the default for
boolean, which is false.
And at end of the definition the interpreter prints:
greatWorld3: Boolean = false
which might have happened either because:
a. the interpreter returns the type of the just created function/variable
by default
OR
b. Because there is a recursive call to greatWorld3, and the interpreter
prints the vale of greatWorld3.
Not sure which one these above 2 is applied?
Next I go into function definition, with *def*
I then move on to define a new variable j with 0, and now replace the
definition of greatWorld3 with a function def as below:
scala> var j = 0
j: Int = 0
scala> def greatWorld3:Boolean = if(j>10) true else {println("Print j:"+j)
| j += 1
| greatWorld3 }
greatWorld3: Boolean
scala> greatWorld3
Print j:0
Print j:1
Print j:2
Print j:3
Print j:4
Print j:5
Print j:6
Print j:7
Print j:8
Print j:9
Print j:10
res46: Boolean = true
scala> j
res47: Int = 11
Output for def block, is all ok.
Thanks for your help and time again.
Regard,
S
On Saturday, May 2, 2015 at 2:36:39 PM UTC+1, martin wrote:
>
> On Sat, May 2, 2015 at 12:57 PM, scalanewbie <[email protected]
> <javascript:>> wrote:
> > Hello All,
> >
> > I am a newbie to scala and getting in grasp with val and def.
> >
> > I noticed that I could write :
> >
> > scala> var greatWorld2 = println("great world")
> >
> > great world
> >
> > greatWorld2: Unit = ()
> >
> >
> > AND
> >
> >
> > scala> def greatWorld() = println("great world")
> >
> > greatWorld: ()Unit
> >
> >
> > Can someone please help me understand the difference?
> >
> >
> > I am aware that var is generally used to define variables and def is
> used to
> > define functions.
> >
> >
> > Now, keeping in mind that greatWorld2 is actually a variable and not a
> > function, I am trying to print it, and I get:
> >
> >
> > scala> greatWorld2
> >
> You are probably surprised that you see no output. That's because
> greatWorld2 returns a unit value (after all, its type is Unit) and the
> REPL does not print unit values. It could print () but it assumes that
> you executed the expression for its side effects only.
>
> Hope this helps
>
> - Martin
>
> >
> > OR, if I try calling it as a function, it throws an error e.g. below
> (which
> > sounds ok as I did not declare this as a function, so was expecting this
> > anyway).
> >
> >
> > scala> greatWorld2()
> >
> > <console>:9: error: Unit does not take parameters
> >
> > greatWorld2()
> >
> >
> >
> > Long story short, difference between var and def , in terms of what the
> > interpreter and or compiler is doing underneath will be very good to
> > understand.
> >
> >
> > Thanks Guys,
> >
> > S
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups
> > "scala-language" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an
> > email to [email protected] <javascript:>.
> > For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
> Martin Odersky
> EPFL
>
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.