Are there any languages ​​that implement generics _well_?

I liked the discussion of Generics Differences , and was wondering if there were any languages ​​that used this feature particularly well.

I really don't like Java List<? extends Foo> List<? extends Foo> for a List things that are Liskov-substitutable for Foo . Why can't List<Foo> cover this?

And frankly, Comparable<? super Bar> Comparable<? super Bar> ?

I also can’t remember my life, why you should never return an array of generics:

 public T[] getAll<T>() { ... } 

I never liked C ++ templates, but that was mainly because none of the compilers could throw out a remote error message for them. Once I actually did make realclean && make 17 times to compile something; I never understood why the 17th time was a charm.

So who really likes to use generics in their favorite language?

+7
language-agnostic generics
source share
6 answers

Haskell pretty well implements parameterization of the constructor type (generics, or parametric polymorphism). Thus, Scala (although sometimes it takes a few hands).

Both of these languages ​​have higher-grade types (constructors of the abstract type aka, type-type polymorphism, or higher-order polymorphism).

See here: Higher Generics

+14
source share

Damn, English doesn't even implement generics. :)

My offset for C #. Mostly because this is what I am using now, and I used them for a good effect.

+7
source share

I think the generics in Java are actually pretty good. List<? extends Foo> is List<Foo> different from List<? extends Foo> List<? extends Foo> , is that when Foo is a subtype of Bar , List<Foo> not a subtype of List<Bar> . If you could consider the List<Foo> object as a List<Bar> , then you could add Bar objects to it that could break things. This will require any system of a reasonable type. Java allows you to get away with considering Foo[] as a subtype of Bar[] , but this forces you to check the runtime, reducing performance. When you return such an array, it makes it difficult for the compiler to know whether to check the execution.

I never had to use lower bounds ( List<? super Foo> ), but I would suggest that they might be useful for returning common values. See covariance and contravariance .

All in all, although I definitely agree with complaints about overly detailed syntax and confusing error messages. Languages ​​with an output type such as OCaml and Haskell are likely to make your work easier, although their error messages can also be confusing.

+7
source share

I will add OCaml to a list that has really generic generics. I agree that the Haskell class classes are really well made, but this is slightly different from the fact that Haskell does not have OO semantics, but OCaml supports OO.

+3
source share

I am using .Net (VB.Net) and I have had no problems using generics. It is mostly painless.

 Dim Cars as List(Of Car) Dim Car as Car For Each Car in Cars ... Next 

There has never been a problem using shared collections, although I have not gone so far as to design any objects that use generics themselves.

+1
source share

I think C # and VB.NET do a great job with generics.

0
source share

All Articles