I eventually took time to enroll to Functional Programming Principles in Scala on Coursera and the lecture about evaluation strategies really shows an aspect in which most mainstream languages are really a pain: Scala has a natural built-in way to specify lazy evaluation of parameter values. It is not the only language doing it, but the syntax is just neat.
In Scala one can expect arguments to be passed already evaluated if the actual value is actually always needed during the execution of the function:
But if one value is needed only for some execution paths one could also specify it. The actual value is only reduced when needed, and if not needed at all the value is simply omitted:
One can see lazy evaluation by creating a producer that never terminates (example from the course slides), it is never actually called so the following terminates:
But Scala still gets it better by having it baked in the language and making its usage transparent to use any of both paradigms: just take the one which applies best. One advantage is that if the caller naturally has a value, for example the operant is a constant and it does not consume resources to get it, it can just call the method with that value (just like
10 in the above example). And the other way round works too, if one was to return a value and returns a supplier, Scala will reduce it until it produces some value (what it does for
loop above and which never terminates).
This may not seem like much, but it makes code much easier to read and that makes our developer life better.