override '.' member access
spir
denis.spir at gmail.com
Wed Jan 26 03:10:24 PST 2011
On 01/26/2011 01:06 AM, Simen kjaeraas wrote:
> spir <denis.spir at gmail.com> wrote:
>
>> On 01/25/2011 10:29 PM, Simen kjaeraas wrote:
>>> spir <denis.spir at 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)
>
> I just found that it is, in fact, unpossible. That is, you can support
> either a = foo.id; or foo.id = a; - not both. This is caused by bug 620:
> http://d.puremagic.com/issues/show_bug.cgi?id=620
I do not really understand the subtleties of this bug, but indeed, I found no
way to use opDispatch to set data members.
In other words, I could implement python's __getattr__ or Lua's __index,
not python's __setattr__ or Lua's __newindex.
EDIT: Could do this as a workaround.
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) () {
writeln("get");
auto p = (name in this.symbols);
if (p)
return *p;
throw new Exception("No symbol called "~name~".");
}
auto opDispatch (string name, Value) (Value value) {
writeln("set ");
auto p = (name in this.symbols);
if (p) {
*p = value;
return;
}
throw new Exception("No symbol called '"~name~"'.");
}
}
unittest {
auto t = new T(1, ["j":2, "k":3]);
writefln("<%s %s %s>", t.i, t.j,t.k);
t.i = 11;
t.j(22);
t.k(33);
writefln("<%s %s %s>", t.i, t.j,t.k);
//~ writeln(t.l); // throws as expected
//~ t.l(44); // throws as expected
t.j = 222; // Error: function
// __trials__.T.opDispatch!("j").opDispatch ()
// is not callable using argument types (int)
}
The last example shows how normal member-set syntax fails. I would like to know
into what
obj.name = val
is rewritten.
Denis
--
_________________
vita es estrany
spir.wikidot.com
More information about the Digitalmars-d-learn
mailing list