Sunday, April 03, 2011

C# Adds Duck Typing, Latin keyword

It’s that time of year when I announce another new C# feature: robust duck typing.
The C# language team chose a syntax that makes duck typing an opt-in feature, you specify it by using the new ‘duck’ keyword. Compare these two type definitions (note bold text):

public class Foo
{
    public int Compare(Foo right) { ... }
}
public anatis Foo
{
    public int Compare(Foo right) { ... }
}

The first case is the familiar class definition we’ve become familiar with. The second case produces a type that implicitly implements the IComparable<T> interface for Foo. The compiler detects that Foo implements the ‘IComparable<Foo>’ pattern. After recognizing this, the compiler adds IComparable<Foo> to the list of interfaces that Foo implements.

Notice the keyword, ‘anatis’.  It’s the word Duck in Latin. Specifically, it’s the female gender form for duck. The team chose a keyword from a dead language, Latin, to avoid possible conflicts with existing code that could arise from using the English ‘duck’. It might be a breaking change for any application that manages wildlife populations. Everyone is confident that there aren’t any ancient Romans using C#. They’d probably prefer the more romantic and flowing syntax of vb.NET anyway. The team chose the female form because, well, implementing an interface and just expecting the compiler to infer the intent seem like female behavior. Men would gladly declare their capabilities to everyone.

The key benefit for developers is that they can concentrate on the behavior they want to implement, rather than worrying about the specific names of interfaces that define those behaviors. (Of course, they still need to remember the signatures of all the pertinent methods and properties, but it is expected that the IDE team will eventually catch up.

I expect this feature to do quite a bit to help developers with scenarios where you have to declare and implement several interfaces. No longer will you have to add those extra declarations and let  the IDE implement stubs for all the methods defined in each interface.

Another benefit to duck typing is that the compiler can determine if extension methods that are in scope would implement an interface, the compiler can add stub methods in the main type, and call the extension method directly. Yes, you can add interface implementations to existing classes by writing extension methods on a duck type!

Of course, the downside is that this means modifying the usings in your class definition can add or remove interfaces.  I’m confident that developers can get this right.
Some community members have raised concerns that this feature code be abused. They fear that people will write code like the following, making it very difficult to determine the interfaces a particular type implements:

public anatis ExtremeDuck
{
    public int IndexOf(string item) { }
    public void Insert(int index, string item) { }
    public void RemoveAt(int index) { } 
    public string this[int index]
    {
        get { } 
        set { }
    }
    public void Add(string item) { }
    public void Clear() { }
    public bool Contains(string item) { }
    public void CopyTo(string[] array, int arrayIndex) { }
    public int Count
    {
        get { }
    }

Read more: Bill Wagner