Any trick for defining an operator overload in a different namespace?
Ali Çehreli
acehreli at yahoo.com
Sat Aug 31 16:01:18 PDT 2013
On 08/31/2013 03:07 AM, Andrej Mitrovic wrote:
> I'm trying to achieve the syntax "opts[...] = 123", rather than using
> the more direct "this[...] = 123". I can use this code:
>
> -----
> class C
> {
> this()
> {
> opts = Opts(this);
> opts["foo"] = 1;
> }
>
> struct Opts
> {
> C c;
>
> void opIndexAssign(T)(T value, string option)
> {
> c.assign(option, value);
> }
> }
>
> Opts opts;
>
> private void assign(string option, int value)
> {
> }
> }
>
> void main()
> {
> auto c = new C();
> }
> -----
>
> But this explicitly stores the 'this' reference in the struct, I was
> wondering if anyone knows of a trick to avoid having to do that. As
> you can tell I just want a more convenient operator-based syntax over
> calling the 'assign' method, but I don't want the operator to live in
> the class space itself (because then I'd have to use "this[...] =
> that", which is a little quirky for my taste).
>
This is the limitation of inner structs' not having an 'outer'
reference, right?
Even if that were automatically available, it would still need a
reference similar to your explicit 'c' reference. I think... :)
Ali
More information about the Digitalmars-d-learn
mailing list