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
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 scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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
scala> val value = { println("Evaluating..."); 123 }
Evaluating...
value: Int = 123
scala> value
res0: Int = 123
scala> value
res1: Int = 123
scala> def method = { println("Evaluating..."); 123 }
method: Int
scala> method
Evaluating...
res2: Int = 123
scala> method
Evaluating...
res3: Int = 123
scala> method
Evaluating...
res4: Int = 123
scala> lazy val lazyValue = { println("Evaluating..."); 123 }
lazyValue: Int = <lazy>
scala> lazyValue
Evaluating...
res5: Int = 123
scala> lazyValue
res6: Int = 123
scala> lazyValue
res7: Int = 123