Hi Barry,<br><br><blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Though Lion is instanceof Animal its not the case that Cage<Lion> instanceof Cage<Animal>. This is counterintuitive but many people mention it.
<div><br></div><div>If it was the case you could write code like this:</div><div><br></div><div><pre style="margin-top:0px;margin-bottom:10px;padding:5px;border:0px;font-size:14px;vertical-align:baseline;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;overflow:auto;width:auto;max-height:600px;line-height:18px;text-align:left"><code style="margin:0px;padding:0px;border:0px;vertical-align:baseline;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif"><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(43,145,175)">Cage</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(43,145,175)">Animal</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">></span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> animals</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">;</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><br>
</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(43,145,175)">Cage</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(43,145,175)">Lion</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">></span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> lions</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">;</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><br>
animals </span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> lions</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">;</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:gray">// This assignment is not allowed</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><br>
animals</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">.</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">add</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">(</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">rat</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">);</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> </span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:gray">// If this executed, we'd have a Rat in a Cage<Lion></span></code></pre>
</div></blockquote><div>Even if you wrote <br><pre style="margin-top:0px;margin-bottom:10px;padding:5px;border:0px;font-size:14px;vertical-align:baseline;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;overflow:auto;width:auto;max-height:600px;line-height:18px;text-align:left"><code style="margin:0px;padding:0px;border:0px;vertical-align:baseline;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif"><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(43,145,175)">Cage</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;color:rgb(43,145,175)">? extends Animal</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">></span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> animals<br></span></code></pre></div><div>It would allow the assignment of lions to animals ...<br><pre style="margin-top:0px;margin-bottom:10px;padding:5px;border:0px;font-size:14px;vertical-align:baseline;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;overflow:auto;width:auto;max-height:600px;line-height:18px;text-align:left"><code style="margin:0px;padding:0px;border:0px;vertical-align:baseline;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif"><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">animals </span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">=</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"> lions</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent">;</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"></span></code></pre><br>... but would still need to disallow the addition of anything under Animal (because it is in a contravariant position).<br> </div><div>That's the reason why in some situations wildcards don't add much benefit despite forcing people to write boilerplate.¹<br><br>The whole situation is made worse by some of the unreadable capture-of error messages produced from javac due to use-site variance and wildcards: https://gist.github.com/3229086<br>(Compare this with the error message from scalac: https://gist.github.com/3229104 ... the main issue is that Java code like that can sleep in the source code for years until someone tries to use it, while in Scala the code is rejected right at the declaration.)<br><br>The stuff javac does and sometimes doesn't is sometimes quite unfortunate. :-(<br><br>¹See also
http://www.slideshare.net/marakana/effective-java-still-effective-after-all-these-years (p. 28)
in which using wildcards for return types is discouraged.<br><br>Bye,<br><br>Simon<br></div>