Interface "indexing"

Prudence via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 6 11:16:01 PDT 2015


On Sunday, 6 September 2015 at 18:11:44 UTC, Kagamin wrote:
> Well, you can have an array of event factories:
>
> IEvent function()[2] factories = [ factory1, factory2 ];
>
> IEvent factory1() { return new Event1(); }
> IEvent factory2() { return new Event2(); }
>
> Then use enum for indexing:
> IEvent e = factories[NumEvent1]();

Yes, I suppose an array would work, but realize that since enum 
is a compile time construct, the dynamic array is not necessary. 
And since your factories are all the time and always will be(if 
you change anything you have to refactor a lot of stuff).

It seems all this stuff could be simplified a great deal. And no 
one said you wouldn't have a switch. I'm not talking about 
creating some tycheyon particles. I'm simply talking about some 
way to hide the details(which, could be done with a string mixin 
but at the cost of not being able to parse them and debug them 
well).




More information about the Digitalmars-d-learn mailing list