[External] : Re: This expression in lambda in early construction context

Ella Ananeva ella.ananeva at oracle.com
Wed Jun 5 20:30:54 UTC 2024


Hi Remi,

Good point! I get it that the value of this is not accessible in lambda body before the superconstructor call.
However, we cannot access the value of this in the early construction context even without lambda:

class Test {
    int a;
    Test() {
        this.a = 1;
        int a = this.a; // compilation error
        super();
    }
}

JEP 482 specifically says that in the early construction context the value of this is not accessible, that we only can assigned the value to the fields of this class. Why should the rules for a lambda be different? I’d say, if we want a special behavior for lambda, it should be described in the specification, shouldn’t it?

Thanks,
Ella

From: Remi Forax <forax at univ-mlv.fr>
Date: Wednesday, June 5, 2024 at 12:33 PM
To: Ella Ananeva <ella.ananeva at oracle.com>
Cc: amber-dev <amber-dev at openjdk.org>
Subject: [External] : Re: This expression in lambda in early construction context


________________________________
From: "Ella Ananeva" <ella.ananeva at oracle.com>
To: "amber-dev" <amber-dev at openjdk.org>
Sent: Wednesday, June 5, 2024 8:45:57 PM
Subject: This expression in lambda in early construction context
Hi,
I understand that JEP 482 allows to refer to this in the early construction context if this expression is in the left part of an assignment expression:

class Test {
    int a;
    Test() {
        this.a = 1;
        super();
    }
}
This compiles just fine (I have JDK 23 build 25).
However, when I try to do the same in a lambda body, I get a compilation error:
interface Foo {
void foo();
}

class Test {
int a;

Test() {
Foo lmb = () -> { this.a = 1;};
super();
    }
}
Test.java:9:27
java: cannot reference this before supertype constructor has been called
Is lambda body some special case?

The lambda capture "this" (it is a parameter of the call to the construction of the lambda), but "this" as a value is only available after the call to the super constructor.


Thank you,
Ella Ananeva

regards,
Rémi

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240605/7b5f824c/attachment-0001.htm>


More information about the amber-dev mailing list