Fred Garvin
Posts: 52
Nickname: fredgarvin
Registered: Jan, 2008
|
|
Re: Simplyfing Java Generics by Eliminating Wildcards
|
Posted: Feb 8, 2008 2:41 PM
|
|
The bottom line is we like a static type system when it *helps* us write code. We tend not to like it so much when it impedes progress by confusing us, as Josh points out in his talk.
Indeed, variance and generics make a complex mixture no matter how you approach it. The approach adopted in Java incorporates what is called use-site variance via wildcards. Use-site variance, essentially, places the burden on the user of generic types (user = you and me) to specify variance constraints. Thus, it is quite surprising Sun went with this approach given generics exist primarily to improve our ability to read and write code using, not writing, libraries of generic types. The wildcard approach also leaves us with some very peculiar "features". For instance, take a peek at java.util.List.indexOf( Object o ). Ask yourself why the argument is of type Object and not E as it should be in a perfect world. I'll leave the answer as an exercise to the reader.
Another approach involves definition-site variance where more of the burden is placed on the generic class author where it belongs. Still, though, there's an awful lot of complexity involved that leaks out into our code.
Alas, at the end of the day variance and generics leaves us with complexity we probably would be better off without -- it's not helping us.
What would help us is trading off a little type-safety for a lot of usability, which IMO is exactly what is proposed here. Parametric covariance a la arrays = bliss.
Fwiw, the best alternative I've seen aside from the superior array aproach is here http://research.microsoft.com/~akenn/generics/ECOOP06.pdf It's still too complex for my taste, however; I hope it never sees the light of day.
|
|