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

Gavin Bierman gavin.bierman at oracle.com
Mon Jun 3 13:06:39 UTC 2024


[Back after a few days off…]

Indeed, Archie and I looked at this, and some other approaches, but in the end we went for the simpler approach. Simpler seemed better - alternatives just seemed to make the spec as obscure as the corner-cases we were trying to address! Anyway, apologies as I didn’t document this explicitly in the spec, which would have been helpful. I will do so, and perhaps add some other editorial notes to make it clearer what we have done.

Thanks,
Gavin

On 30 May 2024, at 23:16, Archie Cobbs <archie.cobbs at gmail.com> wrote:

On Thu, May 30, 2024 at 5:08 AM Maurizio Cimadamore <maurizio.cimadamore at oracle.com<mailto:maurizio.cimadamore at oracle.com>> wrote:
I note that this is effectively equivalent to say that the enclosing instance of the local class is the first enclosing instance that does not appear in a pre-construction context, correct? If we stated that, once and for all (in 8.1.3), couldn't we then avoid the need for having other special rules?

(When trying to play with the JLS I still feel like a child wielding a weapon that's way too heavy for me)

At one point I took a stab at what §15.9.2 might say if you went down that route. It started with something like this:

For any expression or class declaration E, define the first available enclosing instance of E, if any, as follows:
    o If E occurs in a static context, then there is no first available enclosing instance of E.
    o If E is a top-level class declaration, a static class declaration, or an interface declaration, then there is no first available enclosing instance of E.
    o Otherwise, let O be the immediately enclosing class declaration of E.
        o If E occurs in an early construction context of O, then the first available enclosing instance of E is the first available enclosing instance of O, if any.
        o Otherwise, the first available enclosing instance of E is O's instance of this.

What's new here is that the definition is recursive, which seems to be inescapable if you want the rules to match the compiler's actual behavior.

But I agree with Dan that it's simpler to avoid all that and just say when you can and cannot reference the enclosing instances, i.e., remember we only need to specify the language here, not the compilation process.

This works for local and anonymous classes because the entire class, and therefore all references to an enclosing instance from the class, are either in a pre-construction context or not, and so there's never a difference between "the enclosing instance X exists" and "the expression X.this is legal".

Of course member classes are different - the enclosing instance is provided either explicitly or implicitly at construction time, and separate rules apply to handle that.

-Archie

--
Archie L. Cobbs

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20240603/3c09780c/attachment.htm>


More information about the amber-spec-experts mailing list