Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I posted a c# feature request here; however, I do not get a lot of attention there. Therefore I am asking you here, what you think of it.


The in and out keywords in generic type declarations are useful; however, due to their nature, their application is limited to a small number of types. What I am suggesting here, is to be able to use them in declarations of variables, fields, parameters and possibly properties in order to restrict the set of possible operations on any generic type in a dynamic and temporary way. This would considerably increase the cases where co- and contravariance could be used.

Examples

// A List of a more derived type can be passed
public void ReadList(IList<out MyType> list)
{
    MyType item = list[0];  // OK
    list[0] = new MyType(); // DISALLOWED because of "out" keyword!
    list.Add(new MyType()); // DISALLOWED because of "out" keyword!
}

// A List of a less derived type can be passed
public void WriteList(List<in MyType> list)
{
    MyType item = list[0];  // DISALLOWED because of "in" keyword!
    list[0] = new MyType(); // OK
    list.Add(new MyType()); // OK
}

--

UPDATE

Taken these declarations

class LessDerived { }

class MyType : LessDerived { }

class MoreDerived : MyType { }

You could use the methods shown above like this

var listOfLessDerived = new List<LessDerived>();
var listOfMyType = new List<MyType >();
var listOfMoreDerived = new List<MoreDerived>();

ReadList(listOfMyType);
ReadList(listOfMoreDerived);

WriteList(listOfMyType);
WriteList(listOfLessDerived);
share|improve this question
    
Co/contra-variance applies to types, not operations. Your example doesn't involve any subtyping of MyType, so it's not clear how exactly the suggestion applies to covariance as opposed to automatically creating readonly interfaces to types. –  Telastyn Apr 27 '12 at 16:18
    
@Telastyn: Generic types can only be used in a covariant way if the generic parameter appears only as output type, e.g. as result type of methods or in property getters. The opposite is true for generic parameter appearing only as input. Therefore types (like IList<T> or List<T>) where the generic parameter is used as input as well as output type cannot be used in co- or contravariance. If it was either restricted to be used only as input or as output in certain situations, then contra- respectively covariance would apply. By operation I mean either reading (getting) or writing (setting). –  Olivier Jacot-Descombes Apr 27 '12 at 16:29
    
@Telastyn: Please see my update for examples of usage. –  Olivier Jacot-Descombes Apr 27 '12 at 16:40
    
So, you're basically proposing the addition of Java wildcards to C#? –  svick Apr 27 '12 at 16:42
1  
public static void Add<T, U>(List<T> list, U obj) where U : T { list.Add(obj); } –  Telastyn Apr 27 '12 at 18:40

1 Answer 1

up vote 6 down vote accepted

The in and out keywords in generic type declarations are useful; however, due to their nature, their application is limited to a small number of types. What I am suggesting here, is to be able to use them in declarations of variables, fields, parameters and possibly properties in order to restrict the set of possible operations on any generic type in a dynamic and temporary way.

Java has this feature. An interesting fact: the "call site" variance feature in Java and the "declaration site" variance feature in C# were both designed in large part by my colleague Mads Torgersen.

I am asking you here, what you think of it.

It's a reasonable feature with many interesting scenarios. We considered it and rejected it. The benefits did not outweigh the costs.

share|improve this answer
    
I had hoped secretly that Eric would bump into my question. I fully understand your decision. Not only the implementation costs have to be considered but also the costs of learning and mastering new language features. I started with C# 1.0 and had the opportunity to learn new features one by one; however, people new to C# are confronted with a large number of features from the beginning. –  Olivier Jacot-Descombes Apr 30 '12 at 15:00
    
It's interesting to note that Scala has chosen to go the C# way (declaration-site variance), presumably because call-site variance is harder for clients of your classes. –  Jordão Jul 11 '12 at 15:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.