What is the meaning of this?

Joshua Bloch jjb at google.com
Tue Feb 2 14:35:01 PST 2010


Peter,

This is an interesting idea.  It certainly provides a way to access the
function object from within its definition without polluting the surrounding
scope, which is a good thing.  Of course the ability to do non-local returns
scares the crap out of me.  Heck, I feel unclean when I do a labeled break
in the language as it currently exists.

          Josh

P.S.  Your idea still leaves unanswered the question of what "this" *should
*represent in the body of a lambda.

On Sun, Jan 31, 2010 at 1:39 AM, Peter Levart <peter.levart at gmail.com>wrote:

> 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