converting a string function name to an actual function call

oliver oliver.ruebenkoenigREM at web.de
Thu Nov 29 02:58:57 PST 2007


Thanks to everyone for looking into this. Here is how i have done it now.
Oliver

-------------------------------

import std.stdio;

int f1( int a ) { 
    return a+1;
}

int f2( int b ) { 
    return b-10;
}

void insertInTable( char [] name, int function(int) address, inout int function(int) [ char[] ] table) {
    table[ name ] = address;
}

int function(int) readFromTable( char [] name, int function(int) [ char [] ] table ) { 
    if( name in table )
        return table[ name ];

    writefln("Name: ", name ," is not in table."); 
    //fix: some default.
}

int main() {
    int function(int) [ char [] ] funcAddressTable;
    insertInTable("f1", &f1, funcAddressTable);
    insertInTable("f2", &f2, funcAddressTable);

    int i = 1;
    i = f1(i);
    writefln("i: ", i); 
    i = f2(i);
    writefln("i: ", i); 

    auto myF = readFromTable("f2", funcAddressTable);
    i = myF(i);
    writefln("i: ", i); 

    return 0;
}



More information about the Digitalmars-d-learn mailing list