<div dir="ltr"><div>I think Ella may have a valid point from the point of view of the specification - i.e., where in the new spec does it specify that the new LHS field assignment exception doesn't apply within a lambda?</div><div><br></div><div>§6.5.6.1 says:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">If the expression name appears in an early construction context of C (8.8.7.1), then it is the left-hand operand of a simple assignment expression (15.26), and the declaration of the named variable lacks an initializer.</blockquote><div><br></div><div>But the definition of early construction context includes all contained lambdas, subclasses, etc.</div><div><br></div><div>So maybe it should instead say something like:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">If the expression name appears in an early construction context of C
(8.8.7.1), then it is the left-hand operand of a simple assignment
expression (15.26), the declaration of the named variable lacks an initializer, and the expression is not contained in any lambda or class declaration that is contained in C.</blockquote><div><br></div><div>-Archie<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jun 7, 2024 at 4:35 PM Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com">maurizio.cimadamore@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>
<div>
<p>Hi Ella,<br>
the problem here is that, since the lambda needs to refer to
"this", it's as if you need to pass "this" to create the lambda
object.</p>
<p>To make it simpler, you can think of the lambda as a local class
(not entirely accurate, but I think it paints a good analogy for
what's going on here):</p>
<p>class Main {<br>
int a;<br>
Main() {<br>
this.a = 1; //line A<br>
class Foo {<br>
Main this$0;<br>
Foo(Main this$0) { this.this$0 = this$0; }<br>
void run() { this$0.a = 1; }<br>
}<br>
Foo lmb = new Foo(this); // line A<br>
<br>
lmb.run(); //line B<br>
super();<br>
}<br>
</p>
<p><br>
</p>
<p>Like before, in line (A) we create the lambda expression (here
modelled as a local class). Note that the local class wants a
construction parameter, of type Main (since the local class needs
to refer to it). But then, in line (B), we need to supply an
argument of type Main, namely "this". So here we are effectively
reading/accessing "this" before the super constructor has been
called.</p>
<p>The lambda expression, being so compact, hides a bit of the
problem and you are right that it's a bit confusing at first, but
I hope the example above clarifies things a bit.</p>
<p>Cheers<br>
Maurizio<br>
</p>
<div>On 05/06/2024 22:29, Ella Ananeva
wrote:<br>
</div>
<blockquote type="cite">
<p class="MsoNormal"><span style="font-size:11pt">Thanks for
pointing this out, Chen.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">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?
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">class
</span><span style="font-size:10pt;font-family:"Courier New";color:black">Main
</span>
<span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">{<br>
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">int
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(135,16,148)">a</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;<br>
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,98,122)">Main</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">()
{<br>
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">this</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">.a
=
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(23,80,235)">1</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;
//</span><i><span style="font-size:10pt;font-family:"Courier New";color:rgb(140,140,140)">line
A<br>
</span></i><span style="font-size:10pt;font-family:"Courier New";color:black">Foo
lmb
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">=
() ->
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">this</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">.a
=
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(23,80,235)">1</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">;</span><i><span style="font-size:10pt;font-family:"Courier New";color:rgb(140,140,140)">
<u></u><u></u></span></i></p>
<p class="MsoNormal" style="background:white"><i><span style="font-size:10pt;font-family:"Courier New";color:rgb(140,140,140)"> </span></i><span style="font-size:10pt;font-family:"Courier New";color:black">lmb</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">.foo();
//line B<br>
</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(0,51,179)">super</span><span style="font-size:10pt;font-family:"Courier New";color:rgb(8,8,8)">();<br>
}</span></p>
</blockquote>
</div>
</blockquote></div><br clear="all"><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div>