"Olivier Pisano" <olivier.pisano@laposte.net> wrote in message
news:i5oq50$1i0r$1@digitalmars.com...
> Le 02/09/2010 13:38, bearophile a écrit :
>> 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 was sure D did support covariance for the return types of overriden
> methods.
>
The OP was talking about this:
class TemplClass(T) {}
class Base {}
class Derived : Base {}
And then having some way to have TemplClass!Derived considered to be derived
from TemplClass!Base, or the other way around.

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

On 15/10/2010 13:49, Sean Reque wrote:
> Java doesn't have variant generic type parameters. Wildcard types in Java are NOT the same thing and are certainly not more powerful. C# doesn't have wildcard types because it doesn't implement generics with erasure. See http://stackoverflow.com/questions/527766/what-is-the-equivalent-of-java-wildcards-in-c-generics.
What do you mean they are not the same? Yes, they don't work the same
way, their semantics is fairly different, but they are both attempting
to address the same problem: To improve safety/expressiveness in the
type system, with regards to variance in generic type parameters.
Why is C#'s approach to variance is more powerful than Java's, and not
the other way around? ( You're not off to a good start when the article
you mentioned exposes a scenario that Java can express, but C# can't, at
least not clearly. ;) )
And think carefully before you justify the above by saying that C#'s
generic are not erasures (ie, the generic parameters information is
preserver at runtime).
--
Bruno Medeiros - Software Engineer