recursive lambdas

Brian Goetz brian.goetz at oracle.com
Mon Sep 24 08:11:39 PDT 2012


Yes, it works for fields but not for locals.  This is because the 
init-before-use rules for locals are stricter.

On 9/24/2012 10:50 AM, Aleksey Shipilev wrote:
> Let me be more clear. As I understand the spec prohibits this:
>
>     void test() {
>        IntUnaryOperator fib =
>            (n) -> (n < 2) ? 1 : fib.operate(n - 1) + fib.operate(n - 2);
>     }
>
> ...but still allows this (still a recursive lambda):
>
>     public IntUnaryOperator fib =
>           (n) -> (n < 2) ? 1 : fib.operate(n - 1) + fib.operate(n - 2);
>
> ...because the current JLS does not prohibit this. Also, the plain old
> inner classes work as well:
>
>      public IntUnaryOperator fib =
>              new IntUnaryOperator() {
>                  @Override
>                  public int operate(int n) {
>                      return (n < 2) ? 1 :
>                           fib.operate(n - 1) + fib.operate(n - 2);
>                  }
>              };
>
>      public interface IntUnaryOperator {
>          int operate(int v);
>      }
>
>
> Am I correct?
>
> Thanks,
> Aleksey.
>
> On 09/24/2012 06:28 PM, Brian Goetz wrote:
>> We decided to disallow this form for the time being, in part to simplify
>> the metafactory protocol and therefore increase the chance of
>> intrinsification of lambda capture sites.
>>
>> On 9/24/2012 10:01 AM, Aleksey Shipilev wrote:
>>> Hi guys,
>>>
>>> What is our current stance on recursive lambdas? I.e. should this code
>>> considered to be correct?
>>>
>>>       public static IntUnaryOperator fib =
>>>          (n) -> (n < 2) ? 1 : fib.operate(n - 1) + fib.operate(n - 2);
>>>
>>> The current EDR spec says nothing about this, except for "Remove support
>>> for recursive lambdas" in the changelog, but both latest public b56 and
>>> the nightly binary builds of lambda/lambda forest in OpenJDK do accept
>>> this as the correct code.
>>>
>>> The reason I ask is that I keep bugging IDEA guys about their lambda
>>> support, fixing a bug here, there, and everywhere, as this helps people
>>> to start hacking on lambdas and provide the useful feedback:
>>>      http://youtrack.jetbrains.com/issue/IDEA-91986
>>>
>>> I think everyone realizes the spec is in flux, but it is important to
>>> understand what are we leaning towards.
>>>
>>> Thanks,
>>> Aleksey
>>>
>


More information about the lambda-libs-spec-observers mailing list