| Thread overview | |||||
|---|---|---|---|---|---|
|
June 08, 2013 dispatch table; symbolic references | ||||
|---|---|---|---|---|
| ||||
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
| ||||
June 08, 2013 Re: dispatch table; symbolic references | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Emil | 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 | |||
June 10, 2013 Re: dispatch table; symbolic references | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply