Changing definite assignement rules: How?
Gernot Neppert
mcnepp02 at googlemail.com
Wed Oct 20 08:12:58 PDT 2010
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?
More information about the lambda-dev
mailing list