In practice there is no syntactical difference between function arguments that are
T and those that are
Function0[T]. This makes it difficult for a human to understand if an argument is evaluated more than once. I would like to change that, although I am not sure of the best way.
A trivial solution is be rid of by-name syntax.
f(() => arg)
Another solution would be to require special syntax for by-name parameters. An example would be to require
=>, so that
This would make it so that
Function1[Unit, T] have different surface syntax.
=> arg and
arg would type differently, and so the compiler can help find problems when the author meant one versus another.
val x: Function0[Int] = 3
val x: Function0[Int] = => 3
This example makes me think my first idea is better, but it’s only because
() would break up the two
=s. I don’t think Function0 is often used this way. However, there is an upside!
val x = => 3
would be inferred as a Function0.
Please respond with any other ideas or criticisms!