Re:Idiomatic Generics in Go
by bad_user on Tuesday September 30, 2014 @02:00PM
(#8389844)
Generics added complexity to Java because of:
1. if you have sub-typing in the language, then you'll naturally have to think of co/contra-variance, which leads us to ...
2. Java had backwards compatibility concerns, for example they were unwilling to deprecate the standard collections, so they chose to implement use-site variance with wildcards
There are several problems there - first of all you cannot declare variance rules on the classes themselves, even though there are legitimate use-cases for that, like immutable data-structures have no problem in being covariant. Hence generics in Java are much less useful than they would be with declaration-site variance. Then subtyping + variance, especially with wildcards which are basically existential types, is undecidable, so by definition people have a hard time in producing a reliable compiler.
Here's a fun example that could crash a compiler:
class C<P> implements List<List<? super C<C<P>>>> {}
And doesn't that suck?
But seriously, Java's designers could have chosen a better, saner path. Declaration-site variance is so much better. And we aren't talking about really expressive type-systems, with higher-kinded types and type-2 polymorphism, for starters the basics would do just fine.
I mean, Go's standard data-structures are generified. Why not provide the same capability to its users? This situation is exactly like with Java's arrays.
1. if you have sub-typing in the language, then you'll naturally have to think of co/contra-variance, which leads us to ...
2. Java had backwards compatibility concerns, for example they were unwilling to deprecate the standard collections, so they chose to implement use-site variance with wildcards
There are several problems there - first of all you cannot declare variance rules on the classes themselves, even though there are legitimate use-cases for that, like immutable data-structures have no problem in being covariant. Hence generics in Java are much less useful than they would be with declaration-site variance. Then subtyping + variance, especially with wildcards which are basically existential types, is undecidable, so by definition people have a hard time in producing a reliable compiler.
Here's a fun example that could crash a compiler:
And doesn't that suck?But seriously, Java's designers could have chosen a better, saner path. Declaration-site variance is so much better. And we aren't talking about really expressive type-systems, with higher-kinded types and type-2 polymorphism, for starters the basics would do just fine.
I mean, Go's standard data-structures are generified. Why not provide the same capability to its users? This situation is exactly like with Java's arrays.