www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - converting a string function name to an actual function call

reply oliver <oliver.ruebenkoenig web.de.REMOVE> writes:
Hi everyone,

is it possible to program kind of a general function that applies the name of a
function (given as a char [] ) to arguments. The following code does not work
but something in the same spirit.

Thanks once more to this very patient group.
Oliver

-------

import std.stdio;

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

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

int apply(char [] name, int arg) {
    return name(arg);
}

int main() {
    int i = 1;
    i = f1(i);
    writefln("i: ",i);
    i = f2(i);
    writefln("i: ",i);
    i = apply( "f1", i );
    i = apply( "f2", i );
    writefln("i: ",i);
    return 0;
}
Nov 27 2007
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"oliver" wrote
 Hi everyone,

 is it possible to program kind of a general function that applies the name 
 of a function (given as a char [] ) to arguments. The following code does 
 not work but something in the same spirit.
As far as I know, this is not possible. D does not currently support runtime reflection, which is required by this type of function. Only compile-time reflection is possible. -Steve
Nov 27 2007
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Steven Schveighoffer wrote:
 "oliver" wrote
 Hi everyone,

 is it possible to program kind of a general function that applies the name 
 of a function (given as a char [] ) to arguments. The following code does 
 not work but something in the same spirit.
As far as I know, this is not possible. D does not currently support runtime reflection, which is required by this type of function. Only compile-time reflection is possible. -Steve
Flectioned can do it: http://flectioned.kuehne.cn/ of course, it's a hack/workaround rather than a language feature, but it works (on Win32 it only works if you compile with debug info).
Nov 27 2007
parent reply bearophile <bearophileHUGS lycos.com> writes:
Robert Fraser:
 Flectioned can do it:
 http://flectioned.kuehne.cn/
I know noting still about Flectioned, but maybe it can be used to create something similar to the Python doctest (you may not like it, but some people love it)... http://docs.python.org/lib/module-doctest.html bearophile
Nov 28 2007
parent reply oliver <oliver.ruebenkoenigREM web.de> writes:
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;
}
Nov 29 2007
parent reply mandel <oh no.es> writes:
oliver Wrote:

 void insertInTable( char [] name, int function(int) address, inout int
function(int) [ char[] ] table) {
     table[ name ] = address;
 }
[..]
     insertInTable("f1", &f1, funcAddressTable);
     insertInTable("f2", &f2, funcAddressTable);
As a side note. You can also write: void insertInTable(inout int function(int) [ char[] ] table, char [] name, int function(int) address) { table[ name ] = address; } insertInTable("f1", &f1); insertInTable("f2", &f2);
Nov 29 2007
parent mandel <oh no.es> writes:
ok, my previous was wrong.

You can also write:
void insert(int function(int) [ char[] ] table, char [] name, int function(int)
address) {
     table[ name ] = address;
}

funcAddressTable.insert("f1", &f1);
funcAddressTable.insert("f2", &f2); 
Nov 29 2007