the future of c#

PDC2008のC#のセッションを少しだけ見た。(スライドバーを適当に動かして、コードが出てきた部分だけ拾って・・・)
デモで出てきたものの一部をメモ書き程度に。
その他の部分は http://d.hatena.ne.jp/ufcpp/20081028/1225192944 などでチェック!

dynamic

こんな感じのコードが出てきてた

using System.Dynamic;
~~
public class Bag : DynamicObject
{
	Dictionary<string, object> items = new Dictionary<string, object>();

	public override object GetMember( System.Scripting.Actions.GetMemberAction action ) {
		return items[ action.Name ];
	}
	public override void SetMember( System.Scripting.Actions.SetMemberAction action, object value ) {
		items[ action.Name ] = value;
	}

	public XElement ToXml( XName name ) {
		return new XElement( name,
			from x in items select new XElement( x.Key, x.Value )
		);
	}
	public override string ToString() {
		return ToXml( "Bag" ).ToString();
	}
}

class Program
{
	static void Main( string[] args ) {
		dynamic x = new Bag();
		x.ID = 12345;
		x.Name = "Lawn Mower";
		x.Category = "Gardening";
		x.Price = 795.50m;

		var shovel = new { Name = "Shovel", Price = 14.95 };
		Console.WriteLine( x );
		WriteBag( x );
		WriteBag( shovel ); // 匿名型を渡しております!
	}

	// 仮引数の型(?)にもdynamicキーワードが使えるらしい
	private static void WriteBag( dynamic x ) {
		Console.WriteLine( "The price of one " + x.Name + " is " + x.Price );
	}
}

この辺の仕組みちょっとPythonに似てる・・・?

optional/named arguments

何となく次あたりで来るような気がしてた!(というか欲しかっただけ)
こんな感じだった

// 宣言
public StreamReader OpenTextFile(
	string path,
	Encoding encoding = null,
	bool detectEncoding = true,
	int bufferSize = 1024 ){}

// 使い方
OpenTextFile( "foo.txt", Encoding.UTF8 );
OpenTextFile( "foo.txt", Encoding.UTF8, bufferSize: 4096 );

その他

動的キャンペーン(!)の一環として(?)、eval環境が用意されていた。(CsharpEvaluatorクラス?)
デモではread-eval-print-loopを書いて、C#IronPythonのごとくインタラクティブに扱っていた。
Formを表示させて、後からボタンを乗っけたり、そのボタンにイベントを追加したり etc...
これでMacro(Metaprogramming)みたいなことができるね(しないと思うけど)。


動的に走ったのは良いですが(リフレクションは使いづらい)、パフォーマンスはどうなんでしょう。
あと全体に蔓延ると静的と動的がケンカして読みづらいコードになりそうな。ご利用は計画的に。