Draft Spec for Second Preview of Flexible Constructor Bodies (JEP 482)

Archie Cobbs archie.cobbs at gmail.com
Fri Jun 7 22:48:15 UTC 2024


I think one thing we could do to help lower the confusion level is come up
with some less ambiguous "enclosing" terminology.

For example, the term "enclosing instance" is ambiguous. Consider this
class:

$ cat E1.java

class E1 {
  E1() {
    class E2 {
      E2() {
        class E3 {
          void run() {
            System.out.println(E1.this);    // ok
          //System.out.println(E2.this);    // error
            System.out.println(E3.this);    // ok
          }
        }
        new E3().run();
        super();
      }
    }
    new E2();
  }

  public static void main(String[] args) {
    new E1();
  }
}

$ javac --enable-preview --release 24 E1.java && java --enable-preview
-classpath classes E1
E1 at 6537cf78
E1$1E2$1E3 at 3cbbc1e0

Does class E3 have an "enclosing instance"?

It's a trick question....

   - If "yes", then why is E2.this inaccessible?
   - If "no", then why is E1.this accessible?

The compiler's "NOOUTERTHIS" doesn't help. It doesn't mean there's no outer
'this' instance, it just means there's no *immediate* outer 'this'
instance.. and the "outerThisStack" is the stack of outer instances you get
when you skip over the NOOUTERTHIS ones (I think).

Regardless, when looking at the compiler code, there's many mentions of
"enclosing instances" and "outer instances" but it's easy to have the wrong
assumption in your head about what these terms actually mean.

In an ideal world we would all agree to use some standard, non-ambiguous
terminology, e.g.:

   - The phrasing should always be "enclosing instance with respect to D",
   where D is some lexically enclosing class.
   - For a class C, the "enclosing instance of C with respect to D" is
   *defined* if:
   - D is a class (not interface, record, enum, etc.)
      - C is nested within D
      - There is no static class (or interface, record, enum, etc.)
      declaration between C and D (including C itself)
   - For a class C, the "enclosing instance of C with respect to D" is
   *accessible* at some point X in C if:
      - The enclosing instance of C with respect to D is defined
      - X is not in an early construction context of D
   - If C is nested directly within D with no intermediate classes, then
   "enclosing instance of C with respect to D" is also "the immediately
   enclosing instance of C".

-Archie

On Fri, Jun 7, 2024 at 5:13 PM Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:

> On 02/06/2024 22:36, Stephan Herrmann wrote:
> > Do you consider the proposed rules too complex to implement cleanly,
> > or too complex to explain to users?
>
> It's a decent rule and explanation of what the compiler does.


-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20240607/15012ab6/attachment.htm>


More information about the amber-spec-experts mailing list