On 01/26/2011 12:05 AM, spir wrote:
On 01/25/2011 10:29 PM, Simen kjaeraas wrote:
spir <denis.s...@gmail.com> wrote:
Hello,
Cannot find corresponding opSomething method, if any. (opDispatch seems to
specialise for method call.)
Else, how to catch obj.member?
opDispatch is likely what you want. with the @property annotation, it
will readily support obj.member; and obj.member = foo; syntax.
Thank you, Simen, i'll try using opDispatch with @property. But I'm not sure
how to write that concretely. My use case is of a type holding a
string[AnyThing] AA called symbols. Then, I wish to map
obj.id
to
obj.symbols["id"]
(hope I'm clear)
Denis
Right, just understood that you meant, I guess: since D supports properties
with "x.a" syntax mapping to a func call x.a() under the hood, then one can use
opDispatch intended for func calls to dispatch plain data membar access. (woof!).
FWIW: the following works fine:
alias int[string] Symbols;
class T {
int i;
Symbols symbols;
this (int i, Symbols symbols) {
this.i = i;
this.symbols = symbols;
}
auto opDispatch (string name) () {
auto p = (name in this.symbols);
if (p)
return *p;
throw new Exception("No symbol called "~name~".");
}
}
unittest {
auto t = new T(1, ["j":2, "k":3]);
writeln(t.i);
writeln(t.j);
writeln(t.k);
writeln(t.l); // throws as expected
}
Denis
--
_________________
vita es estrany
spir.wikidot.com