On Saturday, 28 April 2012 at 23:37:14 UTC, F i L wrote:
Peter Alexander wrote:
- opDispatch
This is useful and of significant value if used the right
way.
Can you give me an example of it being used the right way?
It can be very useful for jQuery/JSON style dynamic objects:
import std.variant, std.stdio;
struct Dynamic {
Variant[string] vars;
void opDispatch(string key)() @property {
return vars[key];
}
void opDispatch(string key, T)(T value) @property {
vars[key] = value;
}
}
void main() {
auto foo = Dynamic();
foo.a = "A";
foo.b = 11;
writefln("%s, %s", foo.a, foo.b);
}
This is a perfect example of misuse.
Glancing at that code, it looks like foo has two member
variables. It is also not clear that each access involves a
hash-table lookup.
Why abuse well-understood syntax when there are simpler ways to
solve the problem?
I hope you are not actually serious about that '->' part.
I'm serious. I don't like overloaded syntax. foo.bar
shouldn't also mean (*foo).bar -- it causes confusion and
introduces ambiguities when either could work. Combine this
with opDispatch, UFCS and function overloading and your in for
some nasty headaches.
Craziness. What could you possibly gain is there in forcing a
pointer distinction at every point that it's used? We already
declared the variable as a pointer, we don't need to be
reminded of that fact at every line.
It matters when you have things that imitate pointers (e.g. smart
pointers). Those will have their own members, separate from the
pointee's members, so ptr.foo could refer to the smart pointer's
members or the pointee's members.