Re: var vs def

Nick Stanchenko <[email protected]>
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
Hi,

I think it’s easier to see the difference with simple examples.

Values are evaluated immediately (eagerly) and keep the result forever. 
That’s why the REPL doesn’t print “Evaluating...” for subsequent lines.

scala> val value = { println("Evaluating..."); 123 }
Evaluating...
value: Int = 123

scala> value
res0: Int = 123

scala> value
res1: Int = 123

Methods, on the other hand, are evaluated every time you call them. That’s 
why the REPL does not print “Evaluating...” after the first line (we 
haven’t called the method yet), but does so for every subsequent line:

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

There are also lazy values, which are evaluated only the first time they 
are accessed (but not during definition):

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

Variables (“var” keyword) behave more or less like values, but you can 
reassign them.
As already mentioned, in case of methods (“def”), you can also have 
arguments.

With that in mind, when you assign “println("Something")” to a “val”/“var”, 
it gets evaluated immediately, prints to the REPL and the “val”/“var” 
receives the result value, which is “()” of type “Unit”. When you assign it 
to a “def”, with parentheses or not, you get a method that will evaluate 
the statement (i.e. print to REPL) every time you call it.

Regarding functions, empty argument lists, etc, there are many great 
answers on StackOverflow:
http://stackoverflow.com/questions/22747631/scala-function-definitions-and-usage
http://stackoverflow.com/questions/8303817/nine-ways-to-define-a-method-in-scala
http://stackoverflow.com/questions/2529184/difference-between-method-and-function-in-scala

Hope this helps,
Nick

On Saturday, May 2, 2015 at 11:57:51 AM UTC+1, scalanewbie 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
>
>
> 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].
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.