www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dispatch table; symbolic references

reply "Emil" <emilper gmail.com> writes:
need a dispatch table and tried it this way:

class Action {
     string[string] function(string[string] p) action;
     this( string[string] function(string[string] p) action_ref ) {
         this.action = action_ref;
     }
     string[string] execute(string[string] parameters) {
         return this.action(parameters);
     }
}

then

Action[string] dispatch_table = [
     "test1" : new Action(&myapp.index.test),
     "test2" : new Action(&myapp.index.another_test)
];

auto my_first_result = dispatch_table["test1"].execute(["a": 
"aa", "b":"BB"]);
auto my_second_result = dispatch_table["test2"].execute([" some 
string key for test2": "in test 2"]);

This works, but I am wondering if there is another way. Does D 
support symbolic references so I could keep the hashtable in a 
string[string], or even use a string variable holding a class 
name to instantiate a class ?

thank you
Jun 08 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Emil:

 This works, but I am wondering if there is another way. Does D 
 support symbolic references so I could keep the hashtable in a 
 string[string], or even use a string variable holding a class 
 name to instantiate a class ?
There is a create, but I think it's still very limited and I think it needs to be improved: http://dlang.org/phobos/object.html Bye, bearophile
Jun 08 2013
parent "Emil" <emilper gmail.com> writes:
did you mean Object.factory() ? It almost does it, but still have 
to cast the object to the right class (where I can't use a string 
variable it seems) to use it or make all the classes I want to 
instantiate this way inherit from the same interface.

thank you bearophile for pointing me in the right direction ... 
reading object_.d was fun but did not understand everything in 
there and will have to postpone ambitious stuff until I finish 
reading the book.



On Saturday, 8 June 2013 at 12:46:40 UTC, bearophile wrote:
...
 There is a create, but I think it's still very limited and I 
 think it needs to be improved:
 http://dlang.org/phobos/object.html

 Bye,
 bearophile
Jun 10 2013