Interface "indexing"

Prudence via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 6 10:32:08 PDT 2015


Suppose I have an interface

interface X(I)
{

}

Could it be possible for I to be an enum and then be able to 
"select" the specific interface at runtime based on the enum 
value?

I'm trying to avoid code like

switch (i)
{
    case I.myenumval1: return new X!myenumval1wrapper;
    ...
    case I.myenumvaln: return new X!myenumvalNwrapper;
}

Where myenumvalkwrapper are just types used for 
indexing/placeholders. X(I) is sort of like an enum of classes(or 
possibly objects)

enum X
{
    val1: class v1 { },
    val2: class v2 { },
    val3: class v3 { },
}

So if auto x = new X.val2() is the same as auto x = new v2(); And 
more importantly, auto x = new X!y(); where y is of type X(the 
enum part) and has value either val1, val2, val3.

(this example is obviously a little different, but if possible, 
would also solve my problem)

---

This helps with code bloat.

Suppose I have a series events and triggers. I could potentially 
code the specific events in an enum. And to fire the event, 
instead of a huge switch(or essentially the same), one can write 
one line of code or so and have D take care of matching up 
things. (essentially for some enum value I want a corresponding 
type to be associated with it)

I'm sure this would require some type of runtime reflection in 
the mapping or an automation of creating the switch code(duffs 
device?).

Any ideas?


More information about the Digitalmars-d-learn mailing list