Changing definite assignement rules: How?
Rémi Forax
forax at univ-mlv.fr
Wed Oct 20 09:19:50 PDT 2010
Le 20/10/2010 17:12, Gernot Neppert a écrit :
> The document "State of the lambda" brought up the idea to change the
> rules for definite assignment in order to allow self-referential
> lambdas.
>
> This would make the following code valid:
>
> final Runnable r = #{ if(arbitraryCondition()) r.run(); };
>
> Well, changing the grammar is one thing. But could somebody please
> shed some light on how this could ever be implemented?
>
> How can you copy the value of the reference 'r' into the lambda before
> it has been assigned?
>
> A first suggestion would be to let the compiler generate code equivalent to:
>
> final AutoRunnable[] $ref = {null};
> final AutoRunnable r = #{ if(arbitraryCondition()) $ref[0].run(); }
> $ref[0] = r;
>
>
> But that wouldn't work with:
>
> abstract class AutoRunnable implements Runnable
> {
> AutoRunnable()
> {
> run();
> }
>
> }
>
> final AutoRunnable r = #{ if(arbitraryCondition()) r.run(); };
>
> Or, even worse:
>
> abstract class AutoRunnable implements Runnable
> {
> AutoRunnable()
> {
> new Thread(this).start();
> }
>
> }
>
> final AutoRunnable r = #{ if(arbitraryCondition()) r.run(); };
>
> What am I overlooking here?
>
The lambda is generated as an inner class and
the reference 'r' is simply replaced by 'this'.
Rémi
More information about the lambda-dev
mailing list