this should not refer to the lambda
Alex Blewitt
alex.blewitt at gmail.com
Sun Feb 21 11:11:12 PST 2010
On Sun, Feb 21, 2010 at 6:51 PM, Rémi Forax <forax at univ-mlv.fr> wrote:
>> This argument is somewhat tautologous. "I'd have difficulty in explaining
>> it because I don't like what it means".
>
> I have no difficulty to explain it. I don't want to explain it.
I'm not sure that is a technical reason to not include something.
> Alex, try to compile:
> int a =a;
> and take a look to the error message.
Test.java:2: illegal forward reference
int a=a;
^
That's the point - it's an illegal reference. Yet, were 'this' used to
describe only the enclosing object instance (and not the lambda
instance), you wouldn't be able to do recursive lambdas.
This prints out a countdown from n to 0:
#(i) { System.out.println(i); if(i>0) this.(i-1) };
So without 'this' referring to the enclosing lambda, you can't do
this. An earlier proposal would be to have an illegal forward
reference like:
countdown = #(i) { System.out.println(i); if(i>0) countdown.(i-1) }
So, you either allow 'this' as referring to the lambda (to support the
recursive case), or disallow 'this' to refer to the lambda; at which
point, you either disallow recursive cases or use hackery forward
references to enable recursion.
Given that the example I posted earlier lets your refer to any number
of nested lambdas, and that in the most case, the compiler should be
smart enough to resolve the correct enclosing scope where necessary
(i.e. you won't need to put 'this' in that frequently, since it will
work up the stack to find it, much like inner classes work today) -
what exactly is the problem with letting 'this' refer to the lambda?
The alternative seems to be to allow 'this' to refer to the lambda,
and then use 'A.this' when you want to access the enclosing classes
'this'. I doubt many Java programmers would find that difficult,
especially since that's how inner classes work today.
I have yet to see a technical argument of what can be done if you
disallow 'this' as a lambda reference, other than having to type in
n+1 extra characters should you ever want to refer to the enclosing
scope.
Alex
More information about the lambda-dev
mailing list