contravariance

What is the best way to inherit an array that needs to store subclass specific data?

I'm trying to set up an inheritance hierarchy similar to the following: abstract class Vehicle { public string Name; public List<Axle> Axles; } class Motorcycle : Vehicle { } class Car : Vehicle { } abstract class Axle { public int Length; public void Turn(int numTurns) { ... } } class MotorcycleAxle : Axle { public bool W...

Why doesn't inheritance work the way I think it should work?

I'm having some inheritance issues as I've got a group of inter-related abstract classes that need to all be overridden together to create a client implementation. Ideally I would like to do something like the following: abstract class Animal { public Leg GetLeg() {...} } abstract class Leg { } class Dog : Animal { public overrid...

How is Generic Covariance & Contra-variance Implemented in C# 4.0?

I didn't attend PDC 2008, but I heard some news that C# 4.0 is announced to support Generic covariance and contra-variance. That is, List<string> can be assigned to List<object>. How could that be? In Jon Skeet's book C# in Depth, it is explained why C# generics doesn't support covariance and contra-variance. It is mainly for writing se...

Why do we need new keywords for Covariance and Contravariance in C#?

Can someone explain why there is the need to add an out or in parameter to indicate that a generic type is Co or Contra variant in C# 4.0? I've been trying to understand why this is important and why the compiler can't just figure it out.. Thanks, Josh ...

Covariance vs. contravariance

What are the concepts of covariance and contravariance? Given 2 classes, Animal and Elephant (which inherits from Animal), my understanding is that you get runtime errors in .NET if you try and put an Elephant into an array of Animal, which happens because Elephant is "bigger" (more specific) than Animal. But could you assign Animal to...

Generic wildcards in variable declarations in Scala

In Java I might do this: class MyClass { private List<? extends MyInterface> list; public void setList(List<MyImpl> l) { list = l; } } ...assuming that (MyImpl implements MyInterface) of course. What is the analog for this in Scala, when using a Buffer? import java.lang.reflect._ import scala.collection.mutable._ class Sca...

Scala covariance / contravariance question

Following on from this question, can someone explain the following in Scala: class Slot[+T] (var some: T) { // DOES NOT COMPILE // "COVARIANT parameter in CONTRAVARIANT position" } I understand the distinction between T+ and T in the type declaration (it compiles if I use T). But then how does one actually write a class whi...

Should I support Co-/Contravariance for Pub-/Sub-Scenarios in C#3.0?

In my application I am creating a simple event hub that offers something for registering subscribers: Subscribes<EventType>(ISubscriber<EventType> subscriber) // and some other methods for adding subscribers And for publishing events. Publish<EventType>(EventType @event) Quite simple. I want to route Publish<int>(0) to all subscri...

Why is conversion between with different type parameters is NOT allowed?

I just don't get it as it would be so useful to convert one generic container into the other? Stack <IType> stack = new Stack<SomeType>(); ...

What are the benefits of covariance and contravariance?

C# 4.0 is going to support covariance and contravariance. But I don't clearly understand the benefits of this new feature. Can you explain me (clearly) why we need it? ...

How to make a generic class with inheritance?

How can I make the following code work? I don't think I quite understand C# generics. Perhaps, someone can point me in the right direction. public abstract class A { } public class B : A { } public class C : A { } public static List<C> GetCList() { return new List<C>(); } ...

Type variance in .NET Framework 4.0

IEnumerable<T>, IComparable<T> and a few more are now type-variant. IList<T>, ICollection<T> and many others aren't. Why? ...

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism ?

I am trying to figure out the exact meaning of the words Covariance and Contravariance from several articles online and questions on StackOverflow, and from what I can understand, it's only another word for polymorphism. Am I correct with the above statement? Or have I got it wrong ? ...

Co-/contravariance support for non-generic types?

I wonder why the C# team decided not to support co-/contravariance for non-generics, considering they could be just as safe. The question is rather subjective, as I don't expect a team member to respond, but someone may have the insight I (and Barbara Liskov) lack. Lets look at this sample interface: public interface ITest { object Pr...

IList using covariance and contravariance in c#, is this possible?

Hi all would this be possible? (I don't have vs. 2010, so I can't try it myself, sorry) public interface IComplexList<out TOutput, in TInput> where TOutput : TInput { public IEnumerator<TOutput> GetEnumerator(); public void Add(TInput item); } public interface IList<T> : IComplexList<T, T> { } If I get it right, you could us...

Why are ref parameters not contravariant?

This works: EndPoint endPoint = new IPEndPoint(_address, _port); _socket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref endPoint); But this does not: IPEndPoint endPoint = new IPEndPoint(_address, _port); _socket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref endPoint); (Note the type of endPoint) Which seems odd. Why does...

How to declare a method that returns a generic collection of "anything" (C#)

I am using a hierarchy of generic collection classes that derive from an abstract base class to store entity items that also derive from an abstract base class: abstract class ItemBase { } class MyItem : ItemBase { public MyItem() { } } abstract class CollectionBase<T> : Collection<T> where T : ItemBase, new() { } class ...

Understanding Covariance and Contravariance in C# 4.0

I watched a video about it on Channel 9 but I didn't really understand it much. Can someone please give me a simple example about these that's easy to understand? After that maybe how it would be used in practice? ...

Java snippet that causes stack overflow in the compiler or typechecker (javac)?

Yesterday at a seminar the presenter showed a small java program, with 3 classes, featuring both co-variance and contra-variance. When attempting to compile using javac, the type checker will throw a StackOverflowException. The snippet is developed by some guys that work at Microsoft (think one was called Kennedy). Can't find it using...

Generic type inference with interface inheritance (co(ntra)-variance?) in C# 3

I have the following two generic types: interface IRange<T> where T : IComparable<T> interface IRange<T, TData> : IRange<T> where T : IComparable<T> ^---------^ | +- note: inherits from IRange<T> Now I want to define an extension methods for col...