What is the meaning of this?

Peter Levart peter.levart at gmail.com
Sun Jan 31 01:39:55 PST 2010


On Thursday 28 January 2010 20:19:05 Joshua Bloch wrote:
> Entirely banning the use of this in lambada expressions is a very bad idea.
> It amounts to throwing out the baby with the bath water. If you need to get
> your hands on the function object created by a lambda expression, but
> this doesn't
> provide it, you're out of luck.There is no general-purpose workaround. And
> people will be tempted to use workarounds that aren't thread-safe, such as
> stashing the lambda in a field.
> 

If we leave the debate about what lambda expression represents (an instance of a class with 
anonymous method or a referenceable block of code with formal parameters) in the background for 
a moment and just focus on "the baby" as you describe, then if you need to get your hands on the 
function object created by a lambda expression, the following syntax could help:

LambdaExpression:
    '#' Identifier_opt '(' FormalParameters_opt ')' Block
    '#' Identifier_opt '(' FormalParameters_opt ')' '(' Expression ')'

Where "Identifier" represents a final local variable valid in the scope of "Block" or "Expression" 
with the function (or SAM) type of a lambda expression. The factorial example would then read:

#int(int) factorial =  #fact(int i)(i == 0 ? 1 : i * (int) (fact.(i - 1)));

This would enable referencing outer instances of lambda expressions from within nested lambda 
expressions, a feature not possible with anonymous inner classes today.

The optional "Identifier" could be used for other purposes:

    break Identifier;  // non-value-bearing break from the statement lambda
    break Identifier : Expression;  // value-bearing break from the statement lambda

In addition, if we make lambdas Serializable, the identifier could help determine the name of the 
class (or a method if java.dyn.MethodReference is used for implementation) generated by the 
compiler so that moving lambda expression around within the code doesn't break serialization.

Regards, Peter


More information about the lambda-dev mailing list