<html><head><style id="css_styles" type="text/css"><!--blockquote.cite { margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc }
blockquote.cite2 {margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc; margin-top: 3px; padding-top: 0px; }
a img { border: 0px; }
li[style='text-align: center;'], li[style='text-align: center; '], li[style='text-align: right;'], li[style='text-align: right; '] {  list-style-position: inside;}
body { font-family: 'Segoe UI'; font-size: 12pt; }
.quote { margin-left: 1em; margin-right: 1em; border-left: 5px #ebebeb solid; padding-left: 0.3em; }
--></style></head><body><div>I've been looking at the examples, and I find none of them compelling.  It seems to be primarily fighting against the symptoms of another problem: `this` references escaping.</div><div><br /></div><div>The `FilteredSet` example is a good example of why calling an instance method before an object is fully constructed is bad. In this case `HashSet` itself is calling a non-final method `addAll`, instead of a final `internalAddAll` or something similar.  Furthermore, `FilteredSet` is relying on this undocumented behavior, which is bad as this presumably can still change in a future JDK.  Furthermore, even with the current restrictions in place, a `FilteredSet` can be implemented fairly easily, so in itself that's not a good enough reason to relax the super calling requirements.</div><div><br /></div><div>The `BigPositiveValue` example is quite contrived (as you said), and not something I'd ever need to write.  First, i<span>t perhaps feels more natural to check failing conditions before calling super, but as long as construction is prevented, the end result is the same. A smart compiler can re-order these I suppose.  Second, giving the constructors radically different behaviors when numeric input is either `long` or `float` would never pass review. Static factory methods that can be named ("of(long)", "pow2(float)") would be ideally suited to disambiguate these and avoid potential bugs and confusion.</span></div><div><br /></div><div>I find the idea of having better warnings (or even errors) for `this` references escaping much more compelling.  It should flag any method calls that aren't private or final, with implicit or explicit this references.  This would highlight the mistakes made in `HashSet` allowing them to be fixed.</div><div><br /></div><div>--John</div><div><span><br /></span></div><div>------ Original Message ------<br /></div>
<div>From "Archie Cobbs" <<a href="mailto:archie.cobbs@gmail.com">archie.cobbs@gmail.com</a>></div>
<div>To <a href="mailto:amber-dev@openjdk.java.net">amber-dev@openjdk.java.net</a></div>
<div>Cc "Vicente Romero" <<a href="mailto:vromero@openjdk.org">vromero@openjdk.org</a>>; "Brian Goetz" <<a href="mailto:brian.goetz@oracle.com">brian.goetz@oracle.com</a>></div>
<div>Date 31/10/2022 20:18:19</div>
<div>Subject Re: Loosening requirements for super() invocation</div><div><br /></div>
<div id="x9eb7893f222344b"><blockquote cite="CANSoFxuww_e0rg5aE=bij5DVHPjtx6J6nDKi3Yc1gvXLb9teXA@mail.gmail.com" type="cite" class="cite2">
<div dir="ltr"><div dir="ltr"><div>This is an old thread which I've been looking at again lately.</div><div><br /></div><div>To summarize, the idea is to relax the JLS rules about code prior to super()/this() invocation to more closely match what the JVM allows, which is field assignments and any code that doesn't reference 'this'. Relevant issues are <a href="https://bugs.openjdk.org/browse/JDK-8194743">JDK-8194743</a> and <a href="https://bugs.openjdk.org/browse/JDK-8193760">JDK-8193760</a>.<br /></div><div><br /></div><div>In order to make this idea less theoretical, I've prototyped all the required compiler changes. There is also a <a href="https://github.com/archiecobbs/jdk/blob/JDK-8193760/README-JDK-8193760.md">write-up describing them</a>.<br /></div><div><br /></div><div>There are a few interesting subtleties, e.g. relating to initialization blocks, and there is also a sub-question, which is whether to allow invoking super()/this() within a try block (this would have to be under the restriction that any catch or finally clause must not return normally). Currently that's not possible without a small JVM change, which I also think might be worth considering to really round out this new feature. See the writeup for details.</div><div><br /></div><div>To see some examples of what would be allowed, see <a href="https://github.com/archiecobbs/jdk/blob/JDK-8193760/test/langtools/tools/javac/superInit/SuperInitGood.java">this unit test</a>. The compiler changes are <a href="https://github.com/openjdk/jdk/compare/master...archiecobbs:jdk:JDK-8193760">here</a>.</div><br /><div>Thoughts?<br /></div><div><br /></div><div>-Archie<br /></div><div><br /></div></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jan 17, 2019 at 8:42 AM Brian Goetz <<a href="mailto:brian.goetz@oracle.com">brian.goetz@oracle.com</a>> wrote:<br /></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Some things have improved for this feature since we last talked; several verifier issues that this would have pushed on have been resolved.  So it’s moved from the “way too expensive for the benefit” category into the “there are lots of things we can do, is this really what we want to spend our effort and complexity budget on” category.  <br />
<br />
My view on this is that while there’s nothing wrong with it, it’s also a pretty minor wart.  If this fell out of a bigger feature, I’d certainly not object, but I’d rather spend the effort and complexity budget on things that have broader benefit.<br />
<br />
> On Jan 16, 2019, at 5:48 PM, Archie Cobbs <<a href="mailto:archie.cobbs@gmail.com">archie.cobbs@gmail.com</a>> wrote:<br />
> <br />
> I'm curious what are people's current thoughts on loosening the<br />
> requirements for super() invocation in the context of Amber, e.g.:<br />
> <br />
>    public class MyInputStream extends FilterInputStream {<br />
>        public MyInputStream(InputStream in) {<br />
>            if (in == null)<br />
>                throw new IllegalArgumentException("null input");<br />
>            super(in);      // look ma!<br />
>        }<br />
>    }<br />
><br clear="all" /></blockquote></div><br />-- <br /><div dir="ltr">Archie L. Cobbs<br /></div>
</div>
</blockquote></div>
</body></html>