Dynamic contagion, part two

Last time I discussed how “dynamic” tends to spread through a program like a virus: if an expression of dynamic type “touches” another expression then that other expression often also becomes of dynamic type. Today I want to describe one of the least well understood aspects of method type inference, which also uses a contagion…

2

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…

8

Foolish consistency is foolish

Once again today’s posting is presented as a dialogue, as is my wont. Why is var sometimes required on an implicitly-typed local variable and sometimes illegal on an implicitly typed local variable? That’s a good question but can you make it more precise? Start by listing the situations in which an implicitly-typed local variable either…

29

Why not automatically infer constraints?

UPDATE: Whoops! I accidentally set a draft of this article to automatically publish on a day that I was away on vacation. The fact that it was (1) not purple and (2) introduced the topic and then stopped in mid-sentence were both clues that this was an unfinished edit. Sorry about that; I thought I…

27

Uses and misuses of implicit typing

One of the most controversial features we’ve ever added was implicitly typed local variables, aka “var”. Even now, years later, I still see articles debating the pros and cons of the feature. I’m often asked what my opinion is, so here you go. Let’s first establish what the purpose of code is in the first…

54

Why are anonymous types generic?

Suppose you use an anonymous type in C#: var x = new { A = “hello”, B = 123.456 }; Ever taken a look at what code is generated for that thing? If you crack open the assembly with ILDASM or some other tool, you’ll see this mess in the top-level type definitions .class ‘<>f__AnonymousType0`2′<‘<A>j__TPar’,'<B>j__TPar’>…

16

Constraints are not part of the signature

What happens here? class Animal { } class Mammal : Animal { } class Giraffe : Mammal { }class Reptile : Animal { } …static void Foo<T>(T t) where T : Reptile { }static void Foo(Animal animal) { }static void Main() {     Foo(new Giraffe()); } Most people assume that overload resolution will choose the…

122

Why no var on fields?

In my recent request for things that make you go hmmm, a reader notes that you cannot use “var” on fields. Boy, would I ever like that. I write this code all the time: private static readonly Dictionary<TokenKind, string> niceNames =   new Dictionary<TokenKind, string>()   {    {TokenKind.Integer, “int”}, … Yuck. It would be much…

35

Method Type Inference Changes, Part One

I want to start this by discussing the purpose of method type inference, and clearing up some potential misunderstandings about type inference errors. First off though, a brief note on nomenclature. Throughout this series when I say “type inference” I mean “method type inference”, not any of the other forms of type inference we have…

9

Method Type Inference Changes, Part Zero

Back in November I wrote a bit about a corner case in method type inference which does not work as expected or as specified in C# 3.0. A number of people made blog comments, sent me mail, and entered “Connect” issues with additional problems and ideas for how we could improve this algorithm. (Particular thanks…

5