Lambdas (and procs) in Ruby are just objects with a call
method, as in any OO language with functional programming glued on as an afterthought. There are also:
lambda.(<args>)
lambda[<args>]
as syntactic sugar for lambda.call()
.
Lua allows for a __call
meta method, that lets me call an object, and in the background, the arguments (and the table) are handed to a real function that does the actual magic.
Lambdas don't feel like dealing with real functions since they are called with []
or .()
instead of ()
. Why not add a ()
operator that can be overloaded? Then object()
would be translated to mean object.()
, same as with +
, ||
or any other operator.
Wouldn't it make sense to have syntactic sugar that allows calling the lambda directly? Is this more a matter of design philosophy than technical limitations?
Addressing some "problems" (that aren't really problems) that people have brought up:
If a method returns a lambda, what does that method followed by () mean? does it call only the method, or also the returned lambda?
method()
means call method
. If that expression returns a lambda, method()()
would then call said lambda. The opposite would make no sense at all.
If there is a method called foo, and a callable object called foo, what does foo() mean?
Answer: if foo is a method and a variable, what do you expect foo
to evaluate to? The variable. Ruby already does deal with this kind of ambiguity by simply choosing the more intuitive of two options. Thus, if there is a method foo
, foo()
should call that method. If there's a callable method foo
in the same scope, that'd be shadowed by the real function.