Thread overview
converting a string function name to an actual function call
Nov 27, 2007
oliver
Nov 27, 2007
Robert Fraser
Nov 28, 2007
bearophile
Nov 29, 2007
oliver
Nov 30, 2007
mandel
Nov 30, 2007
mandel
November 27, 2007
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;
}

November 27, 2007
"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


November 27, 2007
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).
November 28, 2007
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
November 29, 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;
}

November 30, 2007
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);

November 30, 2007
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);