Scala allows functions with no parameter lists to be invoked without parentheses:
scala> def theAnswer() = 42
theAnswer: ()Int
scala> theAnswer
res5: Int = 42
How would I construct a scala expression that evaluates to the function theAnswer
itself, rather than the result of theAnswer
? Or in other words, how would I modify the expression theAnswer
, so that the result was of type () => Int
, and not type Int
?
It can be done with:
scala> theAnswer _
res0: () => Int = <function0>
From the answer to the similar question:
The rule is actually simple: you have to write the _ whenever the compiler is not explicitly expecting a Function object.
This call will create the new instance every time, because you're "converting" method to function (what is called "ETA expansion").