As far as I understand ,in scala def is used to make an expression be evaluated lazily.
For example:
var num=123;
def i=10000+num;
print(i);
//result 1: ouput 10123
num=456
print(i)
//result 2: output 10456
var num=789
print(i)
//result 3: output 10456
My question is,after var num=789
,why def i=10000+num
wasn't evaluated to 10789.
I guess that after re-declaring the variable num by var num=789
,scala re-create the other item in symbol table with the same symbol num.
Am I right? why the rsult 3 output 10456 instead of 10789.
Thank you.
In Scala def does not stand for lazily evaluated expression, it is function definition. So when you declare def i=10000+num you get the new function. When you then declare var num=789 this new 'num' shadows 'num' used in function i.
This may be possible only in REPL, if you try to make this trick in scope of the method, it won't compile, since var 'num' declared multiple times