This expression in lambda in early construction context

Remi Forax forax at univ-mlv.fr
Wed Jun 5 19:33:37 UTC 2024


> 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/25e349aa/attachment.htm>


More information about the amber-dev mailing list