Browse by Tags

Tagged Content List
  • Blog Post: How do we ensure that method type inference terminates?

    I missed the party. I was all set to be on that massive wave of announcements about TypeScript , and then a family emergency kept me away from computers from Thursday of last week until just now, and I did not get my article in the queue. Suffice to say that I am SUPER EXCITED about TypeScript. Long...
  • Blog Post: He's So Dreamy

    Happy New Year all! It has just been brought to my attention that this blog and the Programmer Ryan Gosling photo blog share at least one reader: I admit it, I LOL'd. In the interests of total accuracy I'd like to point out that the first entry on the blog contains a subtle error : .NET actually supported...
  • Blog Post: Curiouser and curiouser

    Here's a pattern you see all the time in C#: class Frob : IComparable<Frob> At first glance you might ask yourself why this is not a "circular" definition; after all, you're not allowed to say " class Frob : Frob "(*). However, upon deeper reflection that makes perfect sense; a Frob is something...
  • Blog Post: Exact rules for variance validity

    I thought it might be interesting for you all to get a precise description of how exactly it is that we determine when it is legal to put "in" and "out" on a type parameter declaration in C# 4. I'm doing this here because (1) it's of general interest, and (2) our attempt to make a more human-readable...
  • Blog Post: What's the difference between covariance and assignment compatibility?

    I've written a lot about this already, but I think one particular point bears repeating. As we're getting closer to shipping C# 4.0, I'm seeing a lot of documents, blogs, and so on, attempting to explain what "covariant" means. This is a tricky word to define in a way that is actually meaningful to...
  • Blog Post: Some new videos

    Somehow it has happened again; people just keep on recording videos of me and putting them on the internet. In these videos you find out what I look like when lit from above and behind. Kinda spooky. We should have made the room entirely dark and held a flashlight underneath my face. That would be...
  • Blog Post: Why is covariance of value-typed arrays inconsistent?

    Another interesting question from StackOverflow : uint[] foo = new uint[10]; object bar = foo; Console.WriteLine("{0} {1} {2} {3}", foo is uint[], // True foo is int[], // False bar is uint[], // True bar is int[]); // True What the heck is going on here? This program fragment illustrates an interesting...
  • Blog Post: Why do ref and out parameters not allow type variation?

    Here's a good question from StackOverflow : If you have a method that takes an "X" then you have to pass an expression of type X or something convertible to X . Say, an expression of a type derived from X. But if you have a method that takes a "ref X", you have to pass a ref to a variable of type...
  • Blog Post: The void is invariant

    [UPDATES below] A while back I described a kind of variance that we’ve supported since C# 2.0. When assigning a method group to a delegate type, such that both the selected method and the delegate target agree that their return type is a reference type, then the conversion is allowed to be covariant...
  • Blog Post: Representation and Identity

    (Note: not to be confused with Inheritance and Representation .) I get a fair number of questions about the C# cast operator. The most frequent question I get is: short sss = 123; object ooo = sss; // Box the short. int iii = (int) sss; // Perfectly legal. int jjj = (int) (short) ooo; // Perfectly...
  • Blog Post: The Future of C#, Part Four: Yet More Video

    Mads, Chris, Sam and me on Channel Nine. Enjoy.
  • Blog Post: 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...
  • Blog Post: Immutability in C# Part Three: A Covariant Immutable Stack

    Now suppose we had a hypothetical future version of C# in which interface covariance worked, and we wanted a covariant immutable stack. That is, we want to be able to implicitly convert an IStack<Giraffe> to IStack<Mammal> . As we've already discussed, this doesn't make much sense in an array...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: Covariance and Contravariance in C#, Part Three: Method Group Conversion Variance

    Last time I discussed how array covariance is broken in C# (and Java, and a number of other languages as well.) Today, a non-broken kind of variance supported by C# 2.0: conversions from method groups to delegates. This is a more complicated kind of variance, so let me spell it out in more detail. ...
  • Blog Post: Covariance and Contravariance in C#, Part Two: Array Covariance

    C# implements variance in two ways. Today, the broken way. Ever since C# 1.0, arrays where the element type is a reference type are covariant . This is perfectly legal: Animal[] animals = new Giraffe[10]; Since Giraffe is smaller than Animal , and “make an array of” is a covariant operation...
  • Blog Post: Covariance and Contravariance in C#, Part One

    I have been wanting for a long time to do a series of articles about covariance and contravariance (which I will shorten to “variance” for the rest of this series.) I’ll start by defining some terms, then describe what variance features C# 2.0 and 3.0 already support today, and then discuss some ideas...
Page 1 of 1 (23 items)