It's commonly accepted that Java generics failed in some important ways. The combination of wildcards and bounds led to some seriously unreadable code.
However, when I look at other languages, I really can't seem to find a generic type system that programmers are happy with.
If we take the following as design goals of a such a type system:
- Always produces easy-to-read type declarations
- Easy to learn (no need to brush up on covariance, contravariance, etc.)
- maximizes the number of compile-time errors
Is there any language that got it right? If I google, the only thing I see is complaints about how the type system sucks in language X. Is this kind of complexity inherent in generic typing? Should we just give up on trying to verify type safety 100% at compile time?
My main question is which is the language that "got it right" the best with respect to these three goals. I realize that that's subjective, but so far I can't even find one language where not all it's programmers agree that the generic type system is a mess.
Addendum: as noted, the combination of subtyping/inheritance and generics is what creates the complexity, so I'm really looking for a language that combines both and avoids the explosion of complexity.
easy-to-read type declarations
? The third criteria is also ambiguous: for example, I can turn array index out of bounds exceptions into compile time errors by not letting you index arrays unless I can compute the index at compile time. Also, the second criteria rules out subtyping. That's not necessarily a bad thing but you should be aware of what you're asking.Foo<T> where SiameseCat:T
) and that there's no possibility of having a generic type which isn't convertible toObject
. IMHO, .NET would benefit from aggregate types which were similar to structures, but even more bare-boned. IfKeyValuePair<TKey,TValue>
were such a type, then anIEnumerable<KeyValuePair<SiameseCat,FordFocus>>
could be cast toIEnumerable<KeyValuePair<Animal,Vehicle>>
, but only if the type couldn't be boxed.