recursive lambda as a local variable

Brian Goetz brian.goetz at oracle.com
Wed Sep 11 10:16:32 PDT 2013


Its not so much "intentional" as a consequence of language rules that 
predate lambda.

In the first example, r is a field; in the second, it is a local.  What 
the first lambda really is shorthand for is:

     Runnable r = () -> this.r.run();

So this lambda captures 'this', whereas the second lambda captures 'r'. 
  The latter runs into definite-assignment issues, but fields are not 
subject to assignment and flow analysis.

Of course, when you use 'this' from the initializer of a field, you are 
on thin ice.

On 9/11/2013 12:37 PM, Zhong Yu wrote:
> This compiles: (b106)
>
>      class Test
>      {
>          Runnable r = ()->r.run();
>      }
>
> but this doesn't:
>
>      void test()
>      {
>          Runnable r = ()->r.run();
>      }
>
> is that intentional or a bug?
>
> Zhong Yu
>


More information about the lambda-dev mailing list