Fabulous Adventures In CodingEric Lippert's BlogMethod Type Inference Changes, Part Zero- May 28, 2008 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 to nikov for his many fascinating and detailed Connect reports.) As a result, as soon as I had time I embarked upon a detailed review of the method type inference specification and the...http://blogs.msdn.com/ericlippert/archive/2008/05/28/method-type-inference-changes-part-ze... Precedence vs Associativity vs Order- May 23, 2008 Raymond has written about this, I have written about Raymond writing about it, but I still frequently get questions from people who are unclear on the difference between precedence, associativity and evaluation order. I suspect that this confusion arises from the difference between how most people are trained to evaluate arithmetical expressions versus how compilers generate code to evaluate arithmetical expressions. As a child it was drilled into me that the way to evaluate an...http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.... Method Hiding Apologia- May 21, 2008 Here's some back-and-forth from an email conversation I had with a user a while back. Why should one avoid method hiding If there were no advantages and only disadvantages then we would not have added it to the language in the first place. C implements hiding because hiding is frequently useful. I therefore deny the premise of the question. One should not avoid method hiding if it is the right thing to do. But I find method hiding confusing. It lets derived types appear to...http://blogs.msdn.com/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx A Generic Constraint Question- May 19, 2008 Here's a question I get fairly frequently: the user desires to create a generic type Bar<T> constrained such that T is guaranteed to be a Foo<U>, for some U. The way they usually try to write this is class Bar<T> where T : Foo<T> ... which of course then does not work. The user discovers this when they type new Bar<Foo<string>>() and get an error stating that the constraint has been violated. Which it certainly has. The constraint says that T...http://blogs.msdn.com/ericlippert/archive/2008/05/19/a-generic-constraint-question.aspx Reading Code Over the Telephone- May 16, 2008 In my youth I once attended a lecture given by Brian Kernighan on the subject of code quality, which was very influential on my attitudes towards writing legible code. One of the things that Kernighan recommended was to endeavour write code that was so clear that it could be easily read over the phone and understood. Most people find code much easier to comprehend when read than when heard; if you can make it clear enough to be understood when heard, it's probably pretty clear code. I was...http://blogs.msdn.com/ericlippert/archive/2008/05/16/reading-code-over-the-telephone.aspx Mutating Readonly Structs- May 14, 2008 Consider this program which attempts to mutate a readonly mutable struct. What happens struct Mutable private int x; public int Mutate() this.x = this.x + 1; return this.x; class Test public readonly Mutable m = new Mutable(); static void Main(string args) Test t =...http://blogs.msdn.com/ericlippert/archive/2008/05/14/mutating-readonly-structs.aspx Trivial Projections Are (Usually) Optimized Away- May 12, 2008 OK, computers aren't entirely dumb when it comes to LINQ. Here's an example of a place where we're a bit smarter. Consider the following query: IEnumerable<int> query = from n in number_array orderby n select n; Does this get transformed by the compiler into IEnumerable<int> query = number_array.OrderBy(n => n); or IEnumerable<int> query = number_array.OrderBy(n => n).Select( n => n ); This question caused tremendous debate amongst the members of the C design and...http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optim... Computers are dumb- May 9, 2008 A few short takes today, from questions I've received recently about LINQ in C 3.0. The first question was "in the following code, does it really check every single non-negative integer, or does it use the knowledge that once you're beyond ten, you can stop iterating" var smallNumbers = Enumerable.Range(0, int.MaxValue).Where(n => n < 10);foreach (int i in smallNumbers) Console.WriteLine(i);The former. You asked LINQ to Objects to apply a predicate to a sequence..http://blogs.msdn.com/ericlippert/archive/2008/05/09/computers-are-dumb.aspx Covariance and Contravariance, Part Eleven: To infinity, but not beyond- May 7, 2008 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 of C. (See my earlier articles in this series for an explanation of the proposed feature.) Variance is highly useful in mainstream...http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-tw... Protected Semantics, Part Five: More on immutability- May 5, 2008 I asked a second follow-up question back in Part Two: Suppose you wanted to make this hierarchy an immutable collection, where "Add" and "Remove" returned new collections rather than mutating the existing collection. How would you represent the parenting relationship The short answer is "I wouldn't". First off, it sounds an awful lot like what is intended to be represented here is some sort of highly mutable system, where objects are being added and removed from containers. It might be more..http://blogs.msdn.com/ericlippert/archive/2008/05/05/protected-semantics-part-five-more-on... |