<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<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>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>
<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>
</body>
</html>