4 ms·
'code' is actually a function. But because it doesn't take any parameters it's called without '()' at the end.
by nimbix 17y ago
'code' is actually a function. But because it doesn't take any parameters it's called without '()' at the end.
- samstokes 17y agoActually, 'code' isn't a function - that would require different syntax for declaration and usage: def dont(code: () => Unit) = // ... dont { () => println("foo") } It's actually a lazily-evaluated parameter of type Unit - Scala has optional laziness. This means rather than having to pass in a no-arg anonymous function (which requires the "() => expr" syntax as above) you can just pass an expression, and it's evaluated when (and if) needed instead of when it's passed to the "dont" method. Laziness is the reason that this looks like a control structure, and not just a regular old higher-order function.