Loosening requirements for super() invocation
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Jan 26 14:55:23 UTC 2023
Hi Archie,
the JEP is very well written, good job.
I have been bitten in the past by the problems you describe in the JEP,
so I'm very sympathetic with this work.
The examples you have included do a very good job at describing the
"limitations" that we have all gotten (too?) used to. Being able to
dynamically decide which superclass constructor to call is very
powerful, and the examples you have with complex initialization are very
shiny (in a good way :-) ).
One example that I didn't get was the NullPointer vs. IAE - to me this
seems another case where you want to validate the parameter before
passing it to the superclass constructor - e.g. the same as the very
first example. For this reason, this example seems weak to me - and the
JEP would probably be better off with it omitted (unless I missed some
more subtle point, which is possible).
As stated in the goals of the JEP, your aim is to align the JLS with
what the JVMS allows - which is generally a good and noble starting
point. That said, I find that the JEP goes perhaps a little too far in
that direction, especially when it shows allowing initialization of
instance fields _before_ the superclass constructor is called. I
understand that this does not violate the top-down principle, and that
the JVMS caters for it, but seeing that in a program seems odd, and all
sort of questions started popping up in the back of my mind - e.g. what
if the class initialized a protected field in the superclass before the
superclass constructor is called? And, the fact that depending on
whether field initialization is done before or after super leads to
different error messages (as you show in the JEP) seems also potentially
surprising.
My feeling is that it would be better to start simple(r), and, at least
for the time being, not to give instance field initializers any special
treatment. That is, if you refer to `this` before the superclass
constructor has been called, even to set a field, you get an error. I
don't think we would lose too much: as your examples show, there's quite
a lot of "obviously correct" stuff that this JEP would allow, even w/o
descending into more questionable territory (e.g. instance field
initialization). In fact, the only example that would benefit from
assigning fields before the superclass constructor call, is a "bad" case
of escaping this - not sure how much it's worth doing to support this TBH.
Cheers
Maurizio
On 20/01/2023 17:37, Archie Cobbs wrote:
> On Wed, Jan 11, 2023 at 10:27 AM Vicente Romero
> <vicente.romero at oracle.com> wrote:
>
>
>> For the code before super() work (JDK-8194743
>> <https://bugs.openjdk.org/browse/JDK-8194743>), is it OK to file
>> a PR before there is a JEP?
>
> it could be done either way but I think that going for the JEP
> first is usually a better approach
>
>
> OK, JEP filed here: https://bugs.openjdk.org/browse/JDK-8300786
>
> Fire away :)
>
> -Archie
>
> --
> Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230126/c53ad4aa/attachment-0001.htm>
More information about the amber-dev
mailing list