Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

November 2006 - Posts

Mono Migration Analyzer 1.0 Released

Miguel de Icaza has announced the Mono Migration Analyzer - a tool that helps migrating .NET apps to Mono so they could finally become cross-platform. Great stuff and I'm positive I'll be using it soon enough. [via Brad Wilson]

Opinion: Collection Initializers Syntax

Mads Torgersen wrote last month in his weblog about how collections are identified by C# 3.0's new feature - Collection Initializers. You should go read what they are - good stuff. I'd like to address one of the side-topics in his post - overload selection. Let's take Mads's example:

public class Plurals : IDictionary<string,string> { public void Add(string singular, string plural); // implements IDictionary<string,string>.Add public void Add(string singular); // appends an “s” to the singular form public void Add(KeyValuePair<string,string> pair); // implements ICollection<KeyValuePair<string,string>>.Add // } Plurals myPlurals = new Plurals{ “collection”, { “query”, “queries” }, new KeyValuePair(“child”, “children”) };

The last line is converted by the compiler to:

Plurals myPlurals = new Plurals(); myPlurals.Add(“collection”); myPlurals.Add(“query”, “queries”); // <- Marked Line myPlurals.Add(new KeyValuePair(“child”, “children”));

I'd like to address the marked line. As you can see, the line calls the overload that takes a (string, string) tuple. The collection initializer, however, gets what looks to be an array of two strings.

At first, I was confused and did not understand what this meant, so I asked why not just use the simple parentheses, to which Mads replied:

As for the curly syntax for multiple arguments, let it be no secret that parentheses and curlies were both strong candidates. Both are also already overloaded. Omer's comment outlines that clearly for curlies; however, parentheses are also already heavily used; for grouping, invoking, casting etc. Curlies have the advantage of not falsely suggesting that we have tuples in the language.

Since C# is my main language and I believe it should remain consistent, my answer to this was:

You mention invoking and this is exactly the situation here - you're invoking an overload. You can say that the pure mathematical definition of a function dictates that the above pair is a tuple and that the language doesn't have them, but the fact remains that this is a method call and not a type initialization (I'm talking about only the query/queries part) which would make the use of curly brackets inconsistent with the rest of the language (which uses curly brackets for initialization: anonymous types, collection initializers, array initializers, anonymous methods, etc.).

What's your take on this?

Posted: Nov 14 2006, 10:50 PM by Omer van Kloeten | with 2 comment(s)
Filed under: ,
More Posts