covariance

In C#, why can't a List<string> object be stored in a List<object> variable

It seems that a List object cannot be stored in a List variable in C#, and can't even be explicitly cast that way. List<string> sl = new List<string>();List<object> ol;ol = sl; results in Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.Generic.List’ And then... List<string> sl = new List<string...

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...

What languages support covariance on inherited methods' return types?

I discovered that the cause of my problems (see question) was the lack of support in C# for covariance on inherited methods' return types. Now I'm curious what languages support this feature. Accepted answer will be whoever can name the most. EDIT: John Millikin correctly pointed out that lots of dynamic languages support this. I'll ...

how get a vector<Derived*> into a function that expects a vector<Base*> as argument

Hi, Consider these classes, class Base { ... }; class Derived : public Base { ... }; this function void BaseFoo( std::vector<Base*>vec ) { ... } And finally my vector std::vector<Derived*>derived; I want to pass derived to function BaseFoo, but the compiler doesn't let me. How do I solve this, without copying the whole vector...

C# Can I Override with derived types?

Hello, As far as i know it is not possible to do the following in C# 2.0 public class Father { public virtual Father SomePropertyName { get { return this; } } } public class Child : Father { public override Child SomePropertyName { get { return this;...

Is there a way to cast generic lists to lists of interface/base class types?

I'm trying to show someone a use for interfaces in a crazy situation they've created. They have several unrelated objects in lists, and need to perform an operation on two string properties in each object. I'm pointing out that if they define the properties as part of an interface, they can use the interface object as the type for a me...

How can I use covariant return types with smart pointers?

I have code like this: class RetInterface {...} class Ret1: public RetInterface {...} class AInterface { public: virtual boost::shared_ptr<RetInterface> get_r() const = 0; ... }; class A1: public AInterface { public: boost::shared_ptr<Ret1> get_r() const {...} ... }; This code does not compile. In visual stu...

Is it wrong to cast an enumerator of a child class to an enumerator of a parent class?

I've got an error in my build which says: Error 12 Cannot implicitly convert type 'System.Collections.Generic.IEnumerator< BaseClass>' to 'System.Collections.Generic.IEnumerator< IParentClass>'. An explicit conversion exists (are you missing a cast?) Is it wrong to simply cast it away? This is my code: public Dictiona...

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...

C#-Array Covariance In Generic Classes

Hi, I know that C# supports covariance in arrays like this : object[] array = new string[3]; But I'm getting an error when it tries to compile the below code class Dummy<K,T> where T:K { public void foo() { K[] arr = new T[4]; } } It says "Cannot implicitly convert type 'T[]' to 'K[]' " Why I'm getting this er...

In C# 4.0 why can't an out parameter in a method be covariant?

Given this magical interface: public interface IHat<out TRabbit> { TRabbit Take(); } And this class hierarchy: public class Rabbit { } public class WhiteRabbit : Rabbit { } I can now compile this: IHat<WhiteRabbit> hat1 = null; IHat<Rabbit> hat2 = hat1; Which is great. But what if I define the interface differently: public...

C++ covariant templates

I feel like this one has been asked before, but I'm unable to find it on SO, nor can I find anything useful on Google. Maybe "covariant" isn't the word I'm looking for, but this concept is very similar to covariant return types on functions, so I think it's probably correct. Here's what I want to do and it gives me a compiler error: c...

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...

Why is Scala's immutable Set not covariant in its type?

EDIT: Re-written this question based on original answer The scala.collection.immutable.Set class is not covariant in its type parameter. Why is this? import scala.collection.immutable._ def foo(s: Set[CharSequence]): Unit = { println(s) } def bar(): Unit = { val s: Set[String] = Set("Hello", "World"); foo(s); //DOES NOT CO...

Scala API design; a service which returns a Set<I> where I is some interface (abstract/trait)

I have asked quite a few questions about the Scala collection types and how they might actually be used. Consider how I might write some service API/implementation in Java: public interface JavaProductAPI { public Set<IProduct> getProducts(String isin); } And now the impl: public class JavaProductAPIImpl implements JavaProductAP...

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...