To use opDispatch

bearophile bearophileHUGS at lycos.com
Sun May 23 12:32:46 PDT 2010


Simen kjaeraas:

> struct A {
>    int[string] table;
> 
>    auto opDispatch( string name, T... )( T args ) {
>      static if ( T.length == 0 ) {
>        return table[s];
>      } else if ( T.length == 1 ) {
>        table[ name ] = args[0];
>      } else {
>        static assert( false, "Wrong parameter count to opDispatch" );
>      }
>    }
> }

But have you tried to run it? That is similar to one of my first versions, and it doesn't work, I don't know why. This is your code, you can try to run it if you want:

import std.stdio: writeln;

struct A {
    int[string] table;

    auto opDispatch(string name, T...)(T args) {
        static if (T.length == 0) {
            return table[name];
        } else if (T.length == 1) {
            table[name] = args[0];
        } else {
            static assert(false, "Wrong parameter count to opDispatch");
        }
    }
}

void main() {
    A a;
    a.foo = 5;
    writeln(a.foo);
}

Bye and thank you,
bearophile


More information about the Digitalmars-d mailing list