Scala distinguishes between the following things:
- Functions/methods with no parameter lists ("by-name parameter" if a function)
- Functions with one empty parameter list
- Functions with one parameter of type Unit
None of these are equivalent, although as a convenience Scala allows you to elide empty parameter lists. (Incidentally, two empty parameter lists are also not the same.)
So, even though Unit
is written ()
, this is not the same as the function argument parens ()
for a function or method. Instead, think of ()
as a Tuple0
.
So, if you say f: Unit => Int
, what you mean is "f takes one parameter, but it's a really boring parameter because it is Unit
, which must always be the same boring Tuple0
value ()
". What you're writing is really short for f: (Unit) => Int
.
If you say f: () => Int
, then you mean that "f takes no parameters and produces an Int
".
If you say f: => Int
, then you mean that "delay the execution of whatever statement produces an Int
value until we use it in this code (and re-evaluate it each time)". Functionally, this ends up being basically the same as f: () => Int
(and internally is converted into the same Function0
class), but it has a different usage, presumably to allow for a more compact form of closures (you always omit the =>
in the calling code).