type set

bearophile bearophileHUGS at lycos.com
Mon Feb 28 10:07:30 PST 2011


Steven Schveighoffer:

> Not sure if this exists in std.traits or not, but that's where I'd look.

std.typetuple.anySatisfy helps here.

-------

spir:

> Is there any static switch?

foreach done on a typetuple is a static foreach.


> Or any other nicer way to write it than:

I suggest to put the types in a typetuple and use a foreach(i, T, TypeTuple!(t1, t2, ...)), put your values in another typetuple and use the i index to access and return the values.

import std.stdio, std.typetuple;
int mapper(TX)() {
    alias TypeTuple!(int, float, string) Tkeys;
    enum values = [100, 5, 2];
    //alias TypeTuple!(100, 5, 2) Tvalues; // alternative

    foreach (i, T; Tkeys)
        if (is(T == TX))
            return values[i];
    assert(0, "Can't happen");
}
enum r = mapper!float();
static assert(r == 5);
void main() {}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list