www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - do D support something like C# 4.0 co/contra-variance?

reply dennis <dl.soluz gmx.net> writes:
http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-contravariance
Sep 02 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Sep 02 2010
next sibling parent Michal Minich <michal.minich gmail.com> writes:
On Thu, 02 Sep 2010 07:38:24 -0400, bearophile wrote:

 dennis:
 http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-


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

I did not explored this area much, but it seems thanks to duck typed D templates, in some cases template covariance and contravariance is achievable implicitly (see the code example). It is also worth noting that C# does not support more simple OO feature - covariant return types [1], which I really miss when programming in C#. The variance of D templates may be quite hard to specify and implement, given the genericity and complexity of templates. But I'm sure at least some effects are implementable right now - like safe contra/variant cast; and it should be also possible to add variable to types as variance annotations, which are checkable at ct...I might look closer at this some time.. [1] http://www.digitalmars.com/d/2.0/function.html import std.stdio; class Rect { void draw () { writeln ("Rect"); } } class RoundedRect : Rect { override void draw () { writeln ("RoundedRect"); } } class UltraRect : RoundedRect { override void draw () { writeln ("UltraRect"); } } class List (T) { private T[] items; void add (T item) { items ~= item; } T[] getAll () { return items.dup; } } void fill (T) (List!(T) list) { // list.add (new Rect); // errors here: // function List!(RoundedRect).List.add (RoundedRect item) is not callable using argument types (Rect) // cannot implicitly convert expression (new Rect) of type Rect to RoundedRect list.add (new RoundedRect); list.add (new UltraRect); } void drawAll (T) (List!(T) items) { foreach (item; items.getAll()) item.draw(); } void main () { auto l = new List!(RoundedRect); fill (l); drawAll (l); }
Sep 02 2010
prev sibling next sibling parent Michal Minich <michal.minich gmail.com> writes:
On Thu, 02 Sep 2010 07:38:24 -0400, bearophile wrote:

 dennis:
 http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-


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

I just found... http://digitalmars.com/d/2.0/phobos/std_traits.html#isCovariantWith
Sep 02 2010
prev sibling next sibling parent reply Olivier Pisano <olivier.pisano laposte.net> writes:
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. Cheers, Olivier
Sep 02 2010
parent "Nick Sabalausky" <a a.a> writes:
"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.
Sep 02 2010
prev sibling parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
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
Oct 15 2010
parent reply Sean Reque <seanthenewt yahoo.com> writes:
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.
Oct 15 2010
parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
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
Oct 20 2010