Question re: JEP 492 specification
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Wed Dec 11 01:21:09 UTC 2024
What Chen said.
In principle, in this case, creation could be allowed because the local
doesn’t capture. But when discussing the rules, we reached the
conclusion that we didn’t want to specify what “capture” meant - or
classify local classes in terms of what they happen to be capturing. So,
if that is what you were asking, I’d say the behavior you are observing
is deliberate.
Dan correctly pointed out, during an off-line discussion, that what it
would be useful, in some cases, would be the ability to declare a local
/static/ class. In which case you are stating that capture of variables
in the enclosing context by the class is just not a possibility. In that
case, the rules can be relaxed, but on a much more principed basis.
Maurizio
On 10/12/2024 22:47, Chen Liang wrote:
> Hi Archie,
> I think this rule makes sense - Local1 can capture local variables in
> the outer class initializer, in which case the error will be
> legitimate. The extra otherwise does not fix this issue. Adding a
> special case to allow no-capture local classes to be used in more
> nested static contexts seems overkill and error-prone.
>
> Chen
>
>
> On Tue, Dec 10, 2024, 3:57 PM Archie Cobbs <archie.cobbs at gmail.com> wrote:
>
> The proposed JLS changes for JEP 492 are saying that this example
> should no longer compile*:
>
> static {
> class Local1 {
> class Local2 {
> public static void m() {
> new Local1(); // error
> }
> }
> }
> }
>
> but that seems wrong - that exampple has been allowed since
> JDK-8254321, and as it has nothing to do with flexible
> constructors, it should continue to be allowed.
>
> The JEP 492 spec says (new additions in bold):
>
> If /C/ is an inner local class, then:
>
> *
>
> If /C/ occurs in a static context, then /i/ has no immediately
> enclosing instance. *Let /S/ be the nearest |static| method
> declaration, |static| field declaration, or static initializer
> that encloses the declaration of /C/. If the nearest |static|
> method declaration, |static| field declaration, or static
> initializer that encloses the class instance creation
> expression is not /S/, then a compile-time error occurs.*
>
> Should there be an *Otherwise* at the beginning of the newly added
> sentence? (And same thing in the previous paragraph regarding
> anonymous classes)
>
> -Archie
>
> * See JDK-8345953 <https://bugs.openjdk.org/browse/JDK-8345953>
> JEP 492: instantiating local classes in a different static context
> should not be allowed
>
> --
> Archie L. Cobbs
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20241211/a833c74f/attachment-0001.htm>
More information about the amber-spec-experts
mailing list