I was recently playing with a piece of code which relies heavily on function parameters and noticed the following behaviour which I can't quite explain to myself:
// first, a few methods
def a(x: => Any) {}
def b(x:() => Any) {}
// then some helpers
def x = {}
def y() = {}
// these execute the possible combinations
a(x)
b(y)
a(y)
b(x)
The first three work as expected, but the fourth one fails. REPL output for it is
<console>:10: error: type mismatch;
found : Unit
required: () => Any
b(x)
^
For me x
and y
look the same but they obviously aren't. At first I thought it was some kind of property access instead of method being referenced but I couldn't reason that through since a(y)
seems to work just fine - in other words, I can't see the symmetry between the operations.
So, what am I missing?