www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling a method by name.

reply Lurker #5 <lnumber5 klassmaster.com> writes:
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
next sibling parent Kirk McDonald <kirklin.mcdonald gmail.com> writes:

 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
prev sibling next sibling parent Derek Parnell <derek nomail.afraid.org> writes:


 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() { } void command2() { } void command3() { } 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
prev sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
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.



 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