Scala's default evaluation strategy is strict. This means that if you don't do anything special, any variable declaration or arguments of a function call are immediately evaluated. The opposite of a strict evaluation strategy is a lazy evaluation strategy, which means that evaluation is performed only when needed.
The following is a strict variable declaration:
class StrictDemo {val strictVal = {println("Evaluating strictVal")"Hello"}}val strictDemo = new StrictDemo//Evaluating strictVal//strictDemo: StrictDemo = StrictDemo@32fac009
We can see that println
is called immediately. This means that the block at the right side of the assignment is evaluated as soon as the StrictDemo
class is instantiated. If we want to delay the block's evaluation, we have to use the lazy
prefix.