digitalmars.D.learn - converting a string function name to an actual function call
- oliver (26/26) Nov 27 2007 Hi everyone,
- Steven Schveighoffer (5/9) Nov 27 2007 As far as I know, this is not possible. D does not currently support
- Robert Fraser (5/19) Nov 27 2007 Flectioned can do it:
- bearophile (4/6) Nov 28 2007 I know noting still about Flectioned, but maybe it can be used to create...
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
"oliver" wroteHi 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
Steven Schveighoffer wrote:"oliver" wroteFlectioned 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).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
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
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
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
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








mandel <oh no.es>