<div dir="ltr"><div dir="ltr">On Mon, Jun 10, 2024 at 4:05 PM Johannes Spangenberg <<a href="mailto:johannes.spangenberg@hotmail.de">johannes.spangenberg@hotmail.de</a>> wrote:</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>

  
  <div>
    <p>
      </p><blockquote type="cite">and any implementation of withFoo() would
        just have to return an object with type compatible with
        Builder<T>.</blockquote>
    <p></p>
    <p>With these rules, the compiler would allow the following code,
      although it is broken:</p>
    <blockquote>
      <pre>public class Builder { // May have sub-types
    ...
    this.class copy() {
        return new Builder(this);
    }
    ...
}</pre></blockquote></div></blockquote><div>Oops, you're right I didn't think of that. But there's an easy fix :)</div><div><div><br></div><div style="margin-left:40px"><span style="font-family:monospace">public class Builder<T> {</span></div><div style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">    private final Function<this.class, this.class> copyConstructor;</span></div><div style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">    protected Builder(Function<this.class, this.class> copyConstructor) {<br></span></div><div style="margin-left:40px"><span style="font-family:monospace">        this.copyConstructor = copyConstructor;</span></div><div style="margin-left:40px"><span style="font-family:monospace">    }</span></div><div style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">    public this.class copy() {</span></div><div style="margin-left:40px"><span style="font-family:monospace">        return this.copyConstructor.apply(this);</span></div><div style="margin-left:40px"><span style="font-family:monospace">    }</span></div><div style="margin-left:40px"><span style="font-family:monospace">}</span></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><blockquote><pre></pre>
    </blockquote>
    <p>Anyway, there are probably solutions to make it work in most
      situations using more elaborate rules for the type system. I guess
      you could also argue that it is OK that the compiler cannot detect
      the broken code above, and the application should just throw a <font face="monospace">ClassCastException</font>. After all, it
      wouldn't be the only case where broken code is not detect at
      compile time. However, I think such change would require careful
      consideration of different solutions to the problem (and whether
      the problem justifies special syntax and rules).</p>
  </div>

</blockquote></div><div>Agree.. there could be proper checks but it starts to get a little
ugly. The compiler would disallow classes that override a 
supertype containing such methods without overriding each of them. For completeness you'd also want to detect violations at the JVM level as well and throw IncompatibleClassChangeError, etc.</div><br><div>-Archie<br></div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div>