Tuesday, November 4, 2008

C# 4.0 dynamic Intellisense : a letter to the C# designers

I just emailed this to Eric Lippert:

I want to express my concern with the new dynamic language features in C# 4.0.

I think it is important to enable duck-typing along with this feature; possibly by enabling a "fake" cast to an interface. My proposal is below.

 
public class Person
{
public string Name { get; set; }
}

public class Fruit
{
public string Name { get; set; }
}

public interface INamed
{
string Name { get; set; }
}

public static class Program
{
public static void Main()
{
// these types may be defined in any language; possibly from Ruby, etc.
// the cast to INamed is "fake"; used only to enable intellisense since actual member resolution is dynamic
dynamic p = new Person() { Name = "John" } as INamed;
dynamic f = new Fruit() { Name = "Apple" } as INamed;

// would get intellisense after "." provided by the INamed interface
Console.WriteLine(p.Name);
Console.WriteLine(f.Name);
}
}