<div dir="ltr"><div>This relates to <a href="https://bugs.openjdk.org/browse/JDK-8193904" target="_blank">JDK-8193904</a> (if amber-dev is the wrong place to ask about this please let me know; here's an <a href="https://mail.openjdk.org/pipermail/compiler-dev/2022-November/020899.html" target="_blank">earlier thread</a> on compiler-dev).</div><div><br></div><div>The bug asks the question, should we change the JLS so that "MyClass.this" is treated the same way as "this" with respect to definite assignment?</div><div><br></div><div>Here's how the compiler currently behaves:<br></div><div><span style="font-family:monospace"><br></span><div style="margin-left:40px"><span style="font-family:monospace">// This class compiles without error</span><br><span style="font-family:monospace">public class Example1 {</span><br><span style="font-family:monospace"> final int foo;</span><br><span style="font-family:monospace"> public Example1() {</span><br><span style="font-family:monospace"> System.err.println(Example1.this.foo); // no error generated here</span><br><span style="font-family:monospace"> this.foo = 42;</span><br><span style="font-family:monospace"> }</span><br><span style="font-family:monospace">}</span><br><span style="font-family:monospace"></span><br><span style="font-family:monospace">// This class fails to compile</span><br><span style="font-family:monospace">public class Example2 {</span><br><span style="font-family:monospace"> private final int foo;</span><br><span style="font-family:monospace"> public Example2() {</span><br><span style="font-family:monospace"> Example2.this.foo = 42; // "cannot assign a value to final variable</span><span style="font-family:monospace"> foo"</span><br><span style="font-family:monospace"> }</span><br><span style="font-family:monospace">}</span></div></div><div><br></div><div>Presumably the reason for the different treatment is because in general the qualifying expression could be arbitrarily complex and the compiler can't be expected to detect any possible qualification in front of "foo", so why even start down that road?</div><div><br></div><div>For example, instead of</div><div><br></div><div style="margin-left:40px"><span style="font-family:monospace">Example1.this = 42;</span></div><div><br></div><div>you could have:<br></div><div><br></div><div style="margin-left:40px"><span style="font-family:monospace">((Example1)Function.<Object>identity().apply(this)).foo = 42;</span></div><div><br></div><div>Currently JLS §16 says:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><p class="gmail-norm-static"><a name="jls-16-110"></a>
An access to its value consists of the simple name of the variable
(or, for a field, the simple name of the field qualified
by <code class="gmail-literal">this</code>) occurring anywhere in an expression except as
the left-hand operand of the simple assignment operator <code class="gmail-literal">=</code>
(<a class="gmail-xref" href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.26.1" title="15.26.1. Simple Assignment Operator =">§15.26.1</a>).
</p></div></blockquote><div>Some options...</div><div><ol><li>Deprecate the use of "MyClass.this" where "this" would work and start generating a warning</li><li>Update JLS §16 to say "qualified by this or qualified by MyClass.this"<br></li><li>Do nothing and resolve JDK-8193904 as "Won't Fix"</li><li>??<br></li></ol></div><div><div>Thoughts?</div><div><br></div><div>-Archie</div><div><br></div>-- <br><div dir="ltr" data-smartmail="gmail_signature">Archie L. Cobbs<br></div></div></div>