contravariance

How is IEnumerable<T> Contra-variant ?

This post (http://blogs.msdn.com/b/brada/archive/2005/01/18/355755.aspx) says IEnumerable<T> is Contra-variant. However type T is co-variant because it is an out parameter. So in what context is IEnumerable<T> Contra-variant ?? Hope I am not confusing! Thanks for the answers in advance! ...

Contra- and Co-variance - CLR via C#

In the CLR via c# third edition there is an example that I cant seem to make sense of: Invariant Meaning that that generic type parameter cannot be changed. I have shown only invariant generic type parameters so far in this chapter. n Contravariant Meaning that the generic type parameter can change from a class to a cl...

Contravariance isn't working...

public interface IMyControl<in T> where T : ICoreEntity { void SetEntity(T dataObject); } public class MyControl : UserControl, IMyControl<DataObject> // DataObject implements ICoreEntity { void SetEntity(T dataObject); } All fine so far, but why does this create null? var control = LoadControl("~/Controls/MyControl.ascx");...

Scala: issues using functions as first class objects

Hi, I need to have a collection of generic functions, but I can't get it done in the way I like. I created a List[(Any)=>Unit] but as soon as I try to insert a function, for example a String=>Unit I get an error. How could I declare a generic function collection that does not consider parameter and return values types? ...

Why is C# 4.0's covariance/contravariance limited to parameterized interface and delegate types?

Is this a limitation of the CLR or are there compatibility concerns with existing code? Is this related to the messed up variance of delegate combining in C# 4.0? Edit: Would it be possible to have a language using co-/contravariance without that limitation running on the CLR? ...

Examples of good, real-life use-cases for covariance and contravariance in C# 4.0?

Before C# 4.0 came out, I was quite excited about covariance and contravariance. It pandered to my fondness for theoretical correctness! However, now that it’s out, and I’m back to writing normal, everyday, boring code, I’m starting to wonder: did I ever use it yet? And I notice that I haven’t used it consciously. None of the interfaces...

Mnemonic for C# generic types

I often forget if i have to use in or out when defining covarient and contravarient generic types. In java i have the mnemonic PECS (producer extends consumer super) to help me. Do you know a similar mnemonic for c#? ...

Contravariance problems with event properties

Suppose I have a simple EventArgs subclass: class MyArgs : EventArgs { } Consider I have two classes with events: class MyData { public event EventHandler<MyArgs> Method; } class MyObject { public event EventHandler Method; } And a simple program that uses them: static void Main(string[] args){ MyObject o = n...

Why generic interfaces are not co/contravariant by default?

For example IEnumerable<T> interface: public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); } In this interface the generic type is used only as a return type of interface method and not used as a type of method arguments thus it can be covariant. Giving this, can't compiler theoretically infer the va...

Missing compile/runtime error when inlining

Hi, I have the following classes/traits in Scala trait Write[-T] { def add(elem : T); } class ContraListWrapper[T] (var list : List[T]) extends Write[T] { def add(elem : T) = { list = elem :: list } } def bar(list : Write[Number]) = {} Invoking the method with a List of Object or list of Numbers works, thanks to contr...

Why .NET 4 variance for generic type arguments not also for classes?

Possible Duplicates: Why isn't there generic variance for classes in C# 4.0? Why does C# (4.0) not allow co- and contravariance in generic class types? The new .NET 4.0 co- and contravariance for generic type arguments only works for interfaces and delegates. What is the reason for not supporting it for classes too? ...

How would contravariance be used in Java generics?

In Java, covariance allows the API designer to specify that an instance may be generalised as a certain type or any of that type's subtypes. For example: List<? extends Shape> shapes = new ArrayList<Circle>(); // where type Circle extends Shape Contravariance goes the other way. It allows us to specify that an instance may be general...

C# 4.0 Generics and ASP.net MVC

It appears that in C# 4.0, variance specifiers are only applicable to interface types. So let's say I have ViewModel / EditModel classes and a simple hierarchy of models. class MyEditModel<T> where T : Base { ... } class Derived1 : Base { ... } class Derived2 : Base { ... } I have a partial view that accepts a MyEditModel of any type...

Covariance and ContraVariance with LSP

What is the relationship between LSP and Covariance and Contravariance? Is there any relationship? Is LSP a form of Covariance ? ...

General 'map' function for Scala tuples?

Hi, I would like to map the elements of a Scala tuple (or triple, ...) using a single function returning type R. The result should be a tuple (or triple, ...) with elements of type R. OK, if the elements of the tuple are from the same type, the mapping is not a problem: scala> implicit def t2mapper[A](t: (A,A)) = new { def map[R](f: A...