<p>I liked the discussion at <a href="https://stackoverflow.com/questions/31693/differences-in-generics">Differences in Generics</a>, and was wondering whether there were any languages that used this feature particularly well.</p>

<p>I really dislike Java's <code>List&lt;? extends Foo&gt;</code> for a <code>List</code> of things that are Liskov-substitutable for <code>Foo</code>.  Why can't <code>List&lt;Foo&gt;</code> cover that?</p>

<p>And honestly, <code>Comparable&lt;? super Bar&gt;</code>?</p>

<p>I also can't remember for the life of my why you should never return an Array of generics:</p>

<pre><code>public T[] getAll&lt;T&gt;() { ... }
</code></pre>

<p>I never liked templates in C++, but that was mostly because none of the compilers could ever spit out a remotely meaningful error message for them.  One time I actually did a <code>make realclean &amp;&amp; make</code> 17 times to get something to compile; I never did figure out why the 17th time was the charm.</p>

<p>So, who actually <em>likes</em> using generics in their pet language?</p>

[{'answer_id': 50991, 'author': 'Apocalisp', 'author_id': 3434, 'author_profile': 'https://Stackoverflow.com/users/3434', 'pm_score': 5, 'selected': True, 'text': '<p>Haskell implements type-constructor parameterisation (generics, or parametric polymorphism) quite well. So does Scala (although it needs a bit of hand-holding sometimes).</p>\n\n<p>Both of these languages have higher-kinded types (a.k.a. abstract type constructors, or type-constructor polymorphism, or higher-order polymorphism).</p>\n\n<p>See here: <a href="http://lambda-the-ultimate.org/node/2579" rel="nofollow noreferrer">Generics of a Higher Kind</a></p>\n'}, {'answer_id': 50993, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 3, 'selected': False, 'text': "<p>Heck, English doesn't even implement generics well. :)</p>\n\n<p>My bias is for C#.  Mainly because that is what I am currently using and I have used them to good effect.</p>\n"}, {'answer_id': 50997, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 1, 'selected': False, 'text': "<p>I use .Net (VB.Net), and haven't had any problems using generics.  It's mostly painless.</p>\n\n<pre><code>Dim Cars as List(Of Car)\nDim Car as Car\n\nFor Each Car in Cars\n...\nNext\n</code></pre>\n\n<p>Never had any problems using the generic collections, although I haven't gone so far as to design any objects that use generics on my own.</p>\n"}, {'answer_id': 51014, 'author': 'Jay Conrod', 'author_id': 1891, 'author_profile': 'https://Stackoverflow.com/users/1891', 'pm_score': 3, 'selected': False, 'text': '<p>I think the generics in Java are actually pretty good. The reason why <code>List&lt;Foo&gt;</code> is different than <code>List&lt;? extends Foo&gt;</code> is that when <code>Foo</code> is a subtype of <code>Bar</code>, <code>List&lt;Foo&gt;</code> is not a subtype of <code>List&lt;Bar&gt;</code>. If you could treat a <code>List&lt;Foo&gt;</code> object as a <code>List&lt;Bar&gt;</code>, then you could add <code>Bar</code> objects to it, which could break things. Any reasonable type system will require this. Java lets you get away with treating <code>Foo[]</code> as a subtype of <code>Bar[]</code>, but this forces runtime checks, reducing performance. When you return such an array, this makes it difficult for the compiler to know whether to do a runtime check.</p>\n\n<p>I have never needed to use the lower bounds (<code>List&lt;? super Foo&gt;</code>), but I would imagine they might be useful for returning generic values. See <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)" rel="nofollow noreferrer">covariance and contravariance</a>. </p>\n\n<p>On the whole though, I definitely agree with the complaints about overly verbose syntax and confusing error messages. Languages with type inference like OCaml and Haskell will probably make this easier on you, although their error messages can be confusing as well.</p>\n'}, {'answer_id': 51056, 'author': 'airportyh', 'author_id': 5304, 'author_profile': 'https://Stackoverflow.com/users/5304', 'pm_score': 2, 'selected': False, 'text': "<p>I'll add OCaml to the list, which has really <em>generic</em> generics. I agree that Haskell's type classes are really well done, but it's a bit different in that Haskell has no OO semantics, but OCaml does support OO.</p>\n"}, {'answer_id': 51991, 'author': 'Kevin Kershaw', 'author_id': 4292, 'author_profile': 'https://Stackoverflow.com/users/4292', 'pm_score': 0, 'selected': False, 'text': '<p>I think that C# and VB.NET do a good job with generics.</p>\n'}]
['https://Stackoverflow.com/questions/50983', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1190/']