digitalmars.D.learn - Calling a method by name.
- Lurker #5 <lnumber5 klassmaster.com> Jun 26 2007
- Kirk McDonald <kirklin.mcdonald gmail.com> Jun 26 2007
- Derek Parnell <derek nomail.afraid.org> Jun 26 2007
- Robert Fraser <fraserofthenight gmail.com> Jun 27 2007
One more thing... How can I call a method by name? I was looking at the
ClassInfo but I cant' find a way to call a method like this:
class Commands
{
void command1() {}
void command2() {}
void command3() {}
}
Commands cmds = new Commands();
char[] cmd = readLine();
call(cmd, cmd);
Any ideas on how to implement this call() method?
Tnks
Jun 26 2007
Lurker #5 wrote:One more thing... How can I call a method by name? I was looking at the ClassInfo but I cant' find a way to call a method like this: class Commands { void command1() {} void command2() {} void command3() {} } Commands cmds = new Commands(); char[] cmd = readLine(); call(cmd, cmd); Any ideas on how to implement this call() method? Tnks
This is typically a feature seen in dynamic languages like Python or Ruby. In D, the names of methods basically just exist at compile-time. You'll have to implement the dispatch mechanism yourself, perhaps something like: void call(Commands c, char[] cmd) { switch(cmd) { case "command1": c.command1(); break; case "command2": c.command2(); break; case "command3": c.command3(); break; default: assert(false, "bad command!"); } } -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Jun 26 2007
On Tue, 26 Jun 2007 22:28:31 -0400, Lurker #5 wrote:One more thing... How can I call a method by name? I was looking at the ClassInfo but I cant' find a way to call a method like this: class Commands { void command1() {} void command2() {} void command3() {} } Commands cmds = new Commands(); char[] cmd = readLine(); call(cmd, cmd);
The names of the methods and functions are 'lost' during the compilation process, so you might like to set up your own name to function mapping. Something like this I find useful... // -------------------- import std.stdio; // Here are the definition of the known commands. void command1() { writefln("Cmd #1"); } void command2() { writefln("Cmd #2"); } void command3() { writefln("Cmd #3"); } void function()[string] RMap; // List of commands, mapped by name. // Call a command. void call(string cmdname) { if (cmdname in RMap) { writef("Calling '%s': ", cmdname); RMap[cmdname](); } else { throw new Exception("'" ~ cmdname ~ "' is not mapped to a command."); } } // Initialize the Name -> Command mapping static this() { RMap["One"] = &command1; RMap["Two"] = &command2; RMap["Three"] = &command3; } void main() { // Invoke some commands to test it. call("Two"); call("One"); call("One"); call("Three"); call("Four"); // Should fail 'cos not mapped } // --------------- -- Derek (skype: derek.j.parnell) Melbourne, Australia 27/06/2007 1:40:20 PM
Jun 26 2007
http://flectioned.kuehne.cn/ Check there for info about D's runtime reflection capabilities. I'm not _sure_ if flectioned supports it or not, but I think so. Lurker #5 Wrote:One more thing... How can I call a method by name? I was looking at the ClassInfo but I cant' find a way to call a method like this: class Commands { void command1() {} void command2() {} void command3() {} } Commands cmds = new Commands(); char[] cmd = readLine(); call(cmd, cmd); Any ideas on how to implement this call() method? Tnks
Jun 27 2007









Kirk McDonald <kirklin.mcdonald gmail.com> 