The Future of C#, Part One

An attentive reader pointed me at this long thread on a third-party forum where some people are musing about possible future features of C#. There is far, far more here than I possibly have time to respond to in any kind of detail. Also, I am not going to spill the beans. Anders is giving a talk…

30

A Face Made For Email, Part Four

Good heavens, this just keeps on happening to me. If you’re interested in what we’re musing about for future versions of C#, check out this video of Anders Hejlsberg, Scott Wiltamuth, Paul Vick, Mads Torgersen, Matt Warren, Jim Hugunin and, off in one corner, me. Working with this caliber of people every day is a…

12

Covariance and Contravariance, Part Eleven: To infinity, but not beyond

UPDATE: Andrew Kennedy, author of the paper I reference below, was good enough to point out some corrections and omissions, which I have addressed. Thanks Andrew! As I’ve discussed at length in this space, we are considering adding covariance and contravariance on delegate and interface types parameterized with reference types to a hypothetical future version…

22

Covariance and Contravariance in C#, Part Ten: Dealing With Ambiguity

OK, I wasn’t quite done. One more variance post! Smart people: suppose we made IEnumerable<T> covariant in T. What should this program fragment do? class C : IEnumerable<Giraffe>, IEnumerable<Turtle> {    IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator() {         yield return new Giraffe();    }    IEnumerator<Turtle> IEnumerable<Turtle>.GetEnumerator() {         yield return new Turtle();    }// [etc.]} class Program {    static void Main()  {       …

43

Covariance and Contravariance in C#, Part Nine: Breaking Changes

Today in the last entry in my ongoing saga of covariance and contravariance I’ll discuss what breaking changes adding this feature might cause. Simply adding variance awareness to the conversion rules should never cause any breaking change. However, the combination of adding variance to the conversion rules and making some types have variant parameters causes…

34

Covariance and Contravariance in C#, Part Eight: Syntax Options

As I discussed last time, were we to introduce interface and delegate variance in a hypothetical future version of C# we would need a syntax for it. Here are some possibilities that immediately come to mind. Option 1: interface IFoo<+T, -U> { T Foo(U u); } The CLR uses the convention I have been using so…

66

Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?

Suppose we were to implement generic interface and delegate variance in a hypothetical future version of C#. What, hypothetically, would the syntax look like? There are a bunch of options that we could hypothetically consider. Before I get into options though, let’s be bold. What about “no syntax at all”? That is why not just…

21

Covariance and Contravariance in C#, Part Six: Interface Variance

Over the last few posts I’ve discussed how it is possible to treat a delegate as contravariant in its arguments and covariant in its return type. A delegate is basically just an object which represents a single function call; we can do this same kind of thing to other things which represent function calls. Interfaces,…

12

Covariance and Contravariance In C#, Part Five: Higher Order Functions Hurt My Brain

Last time I discussed how we could in a hypothetical future version of C# allow delegate types to be covariant in their return type and contravariant in their formal parameter types. For example, we could have a contravariant action delegate: delegate void Action< -A > (A a); and then have Action<Animal> action1 = (Animal a)=>{…

19

Covariance and Contravariance in C#, Part Four: Real Delegate Variance

In the last two posts I discussed the two kinds of variance that C# already has — array covariance and member-group-to-delegate conversion covariance (on return types) and contravariance (on formal parameter types). Today I want to generalize the latter kind of variance. In C# 3.0 today, even though it is legal to assign a typeless method…

8