To use opDispatch

bearophile bearophileHUGS at lycos.com
Sun May 23 08:48:18 PDT 2010


I have seen this bug report:
http://d.puremagic.com/issues/show_bug.cgi?id=4224

Do you know any way to make the following code work? It can be useful, especially once the int type can be replaced with a good variant type:

struct A {
    int[string] table;

    int opDispatch(string s)() {
        return table[s];
    }

    void opDispatch(string s)(int x) {
        table[s] = x;
    }
}

void main() {
    A a;
    a.i = 10;
    assert(a.i == 10);
}


After about thirty attempts I have seen that this works, but it's bad:

struct A(T) {
    T[string] table;

    T opDispatch(string s)(T x=T.min) {
        if (x != T.min)
            table[s] = x;
        return table[s];
    }
}

void main() {
    A!int a;
    a.i = 10;
    assert(a.i == 10);
}

Bye,
bearophile


More information about the Digitalmars-d mailing list