module pind.samples.ja.memory.memory_10; import std.stdio; interface Animal { string sing(); } class Cat : Animal { string sing() { return "meow"; } } class Dog : Animal { string sing() { return "woof"; } } void main() { string[] toConstruct = [ "Cat", "Dog", "Cat" ]; Animal[] animals; foreach (typeName; toConstruct) { /* 擬似変数__MODULE__は常に現在のモジュールの名前で、 * コンパイル時に文字列リテラルとして使用できる。 */ const fullName = __MODULE__ ~ '.' ~ typeName; writefln("Constructing %s", fullName); animals ~= cast(Animal)Object.factory(fullName); } foreach (animal; animals) { writeln(animal.sing()); } }