我有兴趣学习一种在内部将对象处理为哈希表(如 JavaScript)但可以用强类型包装它们以在设计时提供代码完成/智能感知的好处的语言。以下是我希望这种梦幻语言发挥作用的方式:
public class Lion
{
public void Roar() { Console.WriteLine("Aaarrgghh");}
}
public static Main(string[] args)
{
object myCat = new object(); // just plain object, no type!
// adding a Roar() method to the myCat instance
myCat.Roar += delegate() {Console.WriteLine("Miauew");}
// At this point myCat should qualify to be a Lion.
// So we should be able to successfully duck-type-cast
// her to a lion
Lion myLion = myCat as Lion;
// now the myLion reference is strongly typed,
// so I expect the Intellisense window pop up
// and offer me the Roar() method when I hit the dot after "myLion"
myLion.Roar();
}
我希望这个程序能够无误编译、无异常运行并在控制台上打印 “Miauew”。有没有一种语言可以做到这一点?也许是 C#4.0?