Not recursive lambda works like recursive lambda.

bitter_fox bitterfoxc at gmail.com
Fri Feb 24 07:51:42 PST 2012


Hi,
"SAM sam = () -> () -> sam.invoke()" is not recursive lambda.
However, if sam is a local variable, sam works like recursive lambda.

// This program was compiled by the newest compiler(revision:1254)
public class Main
{
    interface SAM
    {
        SAM invoke();
    }

    static SAM field = () -> () -> field.invoke();

    public static void main(String[] args)
    {
        SAM local = () -> () -> local.invoke();

        field.invoke().invoke(); // Success
        local.invoke().invoke(); // StackOverflowError
    }
}

This is the dis-compiled code by jad:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name:   Main.java


public class Main
{
    static interface SAM
    {
        public abstract SAM invoke();
    }

    public static void main(String args[])
    {
        SAM sam = new SAM() {
            public SAM invoke()
            {
                return new SAM() {
                    public SAM invoke()
                    {
                        return invoke(); // call not sam.invoke() but
this.invoke()
                    }

                    final _cls2 this$0;

                    {
                        this$0 = _cls2.this;
                        super();
                    }
                };
            }
        };
        field.invoke().invoke();
        sam.invoke().invoke();
    }

    static SAM field = new SAM() {
        public SAM invoke()
        {
            return new SAM() {
                public SAM invoke()
                {
                    return Main.field.invoke();
                }

                final _cls1 this$0;

                {
                    this$0 = _cls1.this;
                    super();
                }
            };
        }
    };
}

Regards,
bitter_fox


More information about the lambda-dev mailing list