SIP Proposal: Use parenthesis for call-by-name

If trying to wrap a by-name parameter into a Function0 results in immediate
execution, then how does Future { expression } manage to be asynchronous?
I had a look at the source code of Future.apply, but I didn’t see how it
is different to what has been presented here.

The parameter of valueToByName is not by-name so any expression you (or the compiler) pass to it will already have been evaluated before its result gets wrapped in a Function0.

Possible syntax for folks who want more visibility in evaluation of by-name params:

def f[A](body(): => A) = body()         // require parens, no empty application
def g[A](thunk(): => A) = f(thunk _)    // require underscore, no auto-suspension of evaluation for pass-thru
f { println("the yoozh") }

This would be analogous to a repeated-param type looking different from the inside as outside: in this case, both forms of by-name params, nullary and paramless, look the same to outside invocation, but inside the method, the coder can choose between clean classic look and extra parenny.

Then folks whose functions are too long, so that they can’t see the function signature on the same screen as the parameter references, have a backstop in syntax.

A lint opportunity is to point out when a by-name is referenced twice or in loops or anon funs, and in the case advising to lobby for lazy parameters.

2 Likes