<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:Helvetica;
        panose-1:0 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Aptos;
        panose-1:2 11 0 4 2 2 2 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:12.0pt;
        font-family:"Aptos",sans-serif;}
p.xmsonormal, li.xmsonormal, div.xmsonormal
        {mso-style-name:x_msonormal;
        margin:0in;
        font-size:12.0pt;
        font-family:"Aptos",sans-serif;}
p.xxmsonormal, li.xxmsonormal, div.xxmsonormal
        {mso-style-name:x_xmsonormal;
        margin:0in;
        font-size:11.0pt;
        font-family:"Aptos",sans-serif;}
span.EmailStyle23
        {mso-style-type:personal-reply;
        font-family:"Aptos",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        mso-ligatures:none;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#467886" vlink="#96607D" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt">Thank you so much!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">It makes sense now.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div id="mail-editor-reference-message-container">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-bottom:12.0pt"><b><span style="color:black">From:
</span></b><span style="color:black">Chen Liang <chen.l.liang@oracle.com><br>
<b>Date: </b>Wednesday, June 5, 2024 at 2:59</span><span style="font-family:"Arial",sans-serif;color:black"> </span><span style="color:black">PM<br>
<b>To: </b>Ella Ananeva <ella.ananeva@oracle.com>, amber-dev <amber-dev@openjdk.org><br>
<b>Subject: </b>Re: [External] : Re: This expression in lambda in early construction context<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">> If we have a lambda as a local variable in the constructor, it cannot be invoked outside of the constructor, right?<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">It actually can. In fact, lambdas are just like any objects: wherever the object is accessible, its public methods are accessible, so the lambda can be invoked outside of the constructor if it's passed somewhere
 else, such as being stored in an instance (non-static) field.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">As a result, for now, if you know you executed another method that takes a lambda (like ClassFile.build()) and that lambda is executed, the best bet for you is to declare an array like:<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">int[] value = new int[0]; // uses a mutable object (here a length-1 array) to capture value<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">api.execute(arguments, lambdaArg -> { value[0] = lambdaArg; });<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">this.earlyField = value[0]; // maybe add checks to ensure value is properly initialized<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">If we just allow assignment to early field in a lambda, it's possible for api to send this lambda into an executor that calls the lambda with 5 second delay; that scenario is clearly not "early-construction context"
 and the language rule aims to prevent such misuse.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black">- Chen<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="color:black"><o:p> </o:p></span></p>
</div>
<div class="MsoNormal" align="center" style="text-align:center">
<hr size="0" width="98%" align="center">
</div>
<div id="divRplyFwdMsg">
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:black">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:black"> Ella Ananeva <ella.ananeva@oracle.com><br>
<b>Sent:</b> Wednesday, June 5, 2024 4:29 PM<br>
<b>To:</b> Chen Liang <chen.l.liang@oracle.com>; amber-dev <amber-dev@openjdk.org><br>
<b>Subject:</b> Re: [External] : Re: This expression in lambda in early construction context</span>
<o:p></o:p></p>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="xmsonormal"><span style="font-size:11.0pt">Thanks for pointing this out, Chen.</span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt">Please bear with me for a moment. If we have a lambda as a local variable in the constructor, it cannot be invoked outside of the constructor, right?
</span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="xmsonormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">class
</span><span style="font-size:10.0pt;font-family:"Courier New";color:black">Main </span>
<span style="font-size:10.0pt;font-family:"Courier New";color:#080808">{<br>
    </span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">int
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#871094">a</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">;<br>
    </span><span style="font-size:10.0pt;font-family:"Courier New";color:#00627A">Main</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">() {<br>
        </span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">this</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.a =
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#1750EB">1</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">; //</span><i><span style="font-size:10.0pt;font-family:"Courier New";color:#8C8C8C">line A<br>
        </span></i><span style="font-size:10.0pt;font-family:"Courier New";color:black">Foo lmb
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">= () ->
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">this</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.a =
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#1750EB">1</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">;</span><i><span style="font-size:10.0pt;font-family:"Courier New";color:#8C8C8C">       
</span></i><o:p></o:p></p>
<p class="xmsonormal" style="background:white"><i><span style="font-size:10.0pt;font-family:"Courier New";color:#8C8C8C">        </span></i><span style="font-size:10.0pt;font-family:"Courier New";color:black">lmb</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.foo();
 //line B<br>
        </span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">super</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">();<br>
    }</span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt">The statement in line A assigns a value to a member field before the superconstructor call.</span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt">Lambda also assigns the value to a field and is invoked before the superconstructor call.  </span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt">I’d say the difference in this behavior makes sense only if the compiler doesn’t attempt to initialize the field in line A but tries to initialize it when the lambda is invoked. It’s like line A is just an
 instruction for later, and in line B a real initialization attempt takes place.</span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt">Is this what is happening?</span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="xmsonormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<div id="x_mail-editor-reference-message-container">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="xmsonormal" style="margin-bottom:12.0pt"><b><span style="color:black">From:
</span></b><span style="color:black">Chen Liang <chen.l.liang@oracle.com><br>
<b>Date: </b>Wednesday, June 5, 2024 at 1:43</span><span style="font-family:"Arial",sans-serif;color:black"> </span><span style="color:black">PM<br>
<b>To: </b>Ella Ananeva <ella.ananeva@oracle.com>, amber-dev <amber-dev@openjdk.org><br>
<b>Subject: </b>Re: [External] : Re: This expression in lambda in early construction context</span><o:p></o:p></p>
</div>
<div>
<p class="xmsonormal"><span style="color:black">Hi Ella,</span><o:p></o:p></p>
</div>
<div>
<p class="xmsonormal"><span style="color:black">What do you mean by "Why should the rules for a lambda be different"? Lambda bodies are not part of the early-construction context, as the lambda can be invoked anywhere and anytime after it's created, including
 before the super constructor call completes (such as when it's passed as a parameter to super constructor).</span><o:p></o:p></p>
</div>
<div>
<p class="xmsonormal"><span style="color:black"> </span><o:p></o:p></p>
</div>
<div>
<p class="xmsonormal"><span style="color:black">Best,</span><o:p></o:p></p>
</div>
<div>
<p class="xmsonormal"><span style="color:black">Chen</span><o:p></o:p></p>
</div>
<div class="MsoNormal" align="center" style="text-align:center">
<hr size="0" width="98%" align="center">
</div>
<div id="x_divRplyFwdMsg">
<p class="xmsonormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:black">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:black"> amber-dev <amber-dev-retn@openjdk.org> on behalf of Ella Ananeva
 <ella.ananeva@oracle.com><br>
<b>Sent:</b> Wednesday, June 5, 2024 3:30 PM<br>
<b>To:</b> Remi Forax <forax@univ-mlv.fr>; amber-dev <amber-dev@openjdk.org><br>
<b>Subject:</b> Re: [External] : Re: This expression in lambda in early construction context</span>
<o:p></o:p></p>
<div>
<p class="xmsonormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="xxmsonormal">Hi Remi,<o:p></o:p></p>
<p class="xxmsonormal"> <o:p></o:p></p>
<p class="xxmsonormal">Good point! I get it that the value of this is not accessible in lambda body before the superconstructor call.<o:p></o:p></p>
<p class="xxmsonormal">However, we cannot access the value of this in the early construction context even without lambda:<o:p></o:p></p>
<p class="xxmsonormal"> <o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">class Test {</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">    int a;</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">    Test() {</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">        this.a = 1;</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">        int a = this.a; // compilation error</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">        super();</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">    }</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">}</span><o:p></o:p></p>
<p class="xxmsonormal"> <o:p></o:p></p>
<p class="xxmsonormal">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?<o:p></o:p></p>
<p class="xxmsonormal"> <o:p></o:p></p>
<p class="xxmsonormal">Thanks,<o:p></o:p></p>
<p class="xxmsonormal">Ella<o:p></o:p></p>
<p class="xxmsonormal"> <o:p></o:p></p>
<div id="x_x_mail-editor-reference-message-container">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="xxmsonormal" style="margin-bottom:12.0pt"><b><span style="color:black">From:
</span></b><span style="color:black">Remi Forax <forax@univ-mlv.fr><br>
<b>Date: </b>Wednesday, June 5, 2024 at 12:33</span><span style="font-family:"Arial",sans-serif;color:black"> </span><span style="color:black">PM<br>
<b>To: </b>Ella Ananeva <ella.ananeva@oracle.com><br>
<b>Cc: </b>amber-dev <amber-dev@openjdk.org><br>
<b>Subject: </b>[External] : Re: This expression in lambda in early construction context</span><o:p></o:p></p>
</div>
<div>
<div>
<p class="xxmsonormal"><span style="font-family:"Arial",sans-serif;color:black"> </span><o:p></o:p></p>
</div>
<div>
<p class="xxmsonormal"><span style="font-family:"Arial",sans-serif;color:black"> </span><o:p></o:p></p>
</div>
<div class="MsoNormal" align="center" style="text-align:center"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;color:black">
<hr size="0" width="100%" align="center">
</span></div>
<div>
<blockquote style="border:none;border-left:solid #1010FF 1.5pt;padding:0in 0in 0in 4.0pt;margin-left:3.75pt;margin-top:5.0pt;margin-bottom:5.0pt">
<p class="xxmsonormal"><b><span style="font-family:Helvetica;color:black">From: </span>
</b><span style="font-family:Helvetica;color:black">"Ella Ananeva" <ella.ananeva@oracle.com><br>
<b>To: </b>"amber-dev" <amber-dev@openjdk.org><br>
<b>Sent: </b>Wednesday, June 5, 2024 8:45:57 PM<br>
<b>Subject: </b>This expression in lambda in early construction context</span><o:p></o:p></p>
</blockquote>
</div>
<div>
<blockquote style="border:none;border-left:solid #1010FF 1.5pt;padding:0in 0in 0in 4.0pt;margin-left:3.75pt;margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="xxmsonormal"><span style="color:black">Hi,</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="color:black">I understand that JEP 482 allows to refer to
</span><span style="font-family:"Courier New";color:black">this</span><span style="color:black"> in the early construction context if this expression is in the left part of an assignment expression:</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="color:black"> </span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">class Test {</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">    int a;</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">    Test() {</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">        this.a = 1;</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">        super();</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">    }</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">}</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="color:black">This compiles just fine (I have JDK 23 build 25).</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="color:black">However, when I try to do the same in a lambda body, I get a compilation error:</span><o:p></o:p></p>
<p class="xxmsonormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">interface
</span><span style="font-size:10.0pt;font-family:"Courier New";color:black">Foo </span>
<span style="font-size:10.0pt;font-family:"Courier New";color:#080808">{<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">void
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#00627A">foo</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">();<br>
}<br>
<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">class
</span><span style="font-size:10.0pt;font-family:"Courier New";color:black">Test </span>
<span style="font-size:10.0pt;font-family:"Courier New";color:#080808">{<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">int
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#871094">a</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">;<br>
<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#00627A">Test</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">() {<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:black">Foo lmb
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">= () -> {
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">this</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.a =
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#1750EB">1</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">;};<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">super</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">();<br>
    }<br>
}</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">Test.java:9:27</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="font-family:"Courier New";color:black">java: cannot reference this before supertype constructor has been called</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="color:black">Is lambda body some special case?
</span><o:p></o:p></p>
</div>
</blockquote>
<div>
<p class="xxmsonormal"><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black"> </span><o:p></o:p></p>
</div>
<div>
<p class="xxmsonormal"><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black">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.</span><o:p></o:p></p>
</div>
<div>
<p class="xxmsonormal"><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black"> </span><o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #1010FF 1.5pt;padding:0in 0in 0in 4.0pt;margin-left:3.75pt;margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="xxmsonormal"><span style="color:black"> </span><o:p></o:p></p>
<p class="xxmsonormal"><span style="color:black">Thank you,</span><o:p></o:p></p>
<p class="xxmsonormal"><span style="color:black">Ella Ananeva</span><o:p></o:p></p>
</div>
</blockquote>
<div>
<p class="xxmsonormal"><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black"> </span><o:p></o:p></p>
</div>
<div>
<p class="xxmsonormal"><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black">regards,</span><o:p></o:p></p>
</div>
<div>
<p class="xxmsonormal"><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black">Rémi</span><o:p></o:p></p>
</div>
<div>
<p class="xxmsonormal"><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black"> </span><o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>