型の別名
C#では同一ファイル内に限って、
using int32 = System.Int32;
のように型に別名を付けられますが、他のアセンブリから利用するようなことは出来ません。一方、NemerleではC/C++のtypedefのようなモノがサポートされています。
次のようにtypeを使って型の別名を定義出来ます。
public type int8 = System.SByte; public type int16 = System.Int16; public type int32 = System.Int32; public type int64 = System.Int64; public type intarr = System.Collections.Generic.List[int32];
勿論、このコードはコンパイルしてDLLにして他から利用することが出来ます。例えば、
mutable x : int32; x = 10; System.Console.WriteLine (x.GetType ()); System.Console.WriteLine (x); def a = intarr(); a.Add (20); a.Add (30); System.Console.WriteLine (a.GetType ()); foreach (x in a) System.Console.WriteLine (x); /* System.Int32 10 System.Collections.Generic.List`1[System.Int32] 20 30 */
Generic型などの場合、別名を付けると楽ですね。どんな原理かと言いますと、typeで宣言された型は継承による似非typedefではなく、カスタム属性を持ったインタフェースとなります。で、この属性を見てコンパイラがコンパイル時にコードを元の型に置き換えているようです。