On 02/09/2010 12:38, bearophile wrote:
dennis:
http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-contravariance

I think D2 doesn't support those things yes. But they are useful and may be 
added to D3.

Bye,
bearophile

I haven't read the whole C# article, but I think the way its done in Java seems better than the C# design: it's more powerful and yet more simple.

In Java you can have variance with classes as well, not just with interfaces. But more importantly, in Java the generic parameter can automatically be "covariant" or "contravariant" (as per the same sense that the article uses). So for example in Java if you create a class such as ArrayList<T> then it is already implicitly convertible to
ArrayList<? extends T> and ArrayList<? super T>
which are equivalent in C# to something like
ArrayList<out T> and ArrayList<in T> respectively.
With ArrayList<? extends T> you can get elements from the list (typed as T), but you can't put elements. And with ArrayList<? super T> you can put elements in the list (typed as T), and you can also get elements, but typed as Object.
But the key thing is that you don't have to define these extra interfaces.

Imagine for example a dictionary/map class:
Map<KEY, VALUE>
Whereas in Java you automatically get variance in all possible 4 combinations, it seems to me in C# you would have to define all 4 interfaces... clearly not ideal.

Plus, in Java methods can be also be parameterized with generics, which has several interesting and useful usages...


In any case this matters little for D. It would be mildly nice to have, yes, but it would be incredibly complicated to so in a language as complex as D. There is already a lot of chunkiness and design issues with the new type modifiers (const, shared, pure, etc.), and adding yet more type stuff, especially with something as complex as covariance/contravariance would be inane with a major redesign of D's typesystem. It might something not so much for D3, as for D4... :-X


--
Bruno Medeiros - Software Engineer

Reply via email to