Container Contravariance was Re: [typing] Type-erasure re generics

Jesse Phillips jessekphillips+D at gmail.com
Wed Sep 29 11:07:07 PDT 2010


> On Wednesday, September 29, 2010 10:18:17 Pelle wrote:
> > On 09/29/2010 05:53 PM, Jesse Phillips wrote:
> > > The only benefit, which should be solve in another manner is having this
> > > code work:
> > > 
> > > class A {}
> > > class B:A {}
> > > 
> > > class Container(T) {}
> > > 
> > > void main() {
> > > 
> > >      Container!(A) a = new Container!(B)();
> > > 
> > > }
> > 
> > Sorry for falling off topic, but that code shouldn't work.
> > 
> > a.insert(new A)

I will first quote Wikipedia about C#[1]:

"For example, in C# 3.0 generic parameters did not support co or contravariance; List<A> was not equivalent to List<TypeDerivedFromTypeA> as one might intuit; however, this is now supported in C# 4.0, though standard arrays have always supported covariance & contravariance since .NET was introduced."

Then I will show an example that does compile[2]:

void main() {
    A[] = [new B(), new A()];
}

1. http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)
2. http://ideone.com/ZzDTs


More information about the Digitalmars-d mailing list