| Thread overview | |||||
|---|---|---|---|---|---|
|
August 31, 2013 Any trick for defining an operator overload in a different namespace? | ||||
|---|---|---|---|---|
| ||||
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).
| ||||
August 31, 2013 Re: Any trick for defining an operator overload in a different namespace? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
| |||
September 01, 2013 Re: Any trick for defining an operator overload in a different namespace? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 9/1/13, Ali Çehreli <acehreli@yahoo.com> wrote:
> This is the limitation of inner structs' not having an 'outer' reference, right?
Right, but this was just a workaround. Anyway I did just realize I can use opDispatch for this:
-----
class C
{
this()
{
this.opts["foo"] = 1;
}
private auto opDispatch(string field : "opts")()
{
return this;
}
private void opIndexAssign(T)(T value, string option)
{
assign(option, value);
}
private void assign(string option, int value)
{
}
}
-----
It might seem a little funny that the only thing it does is just returns 'this', which we already had. But I wanted to avoid writing 'this["foo"] = 1;'. Well at the end of the day this may or may not be what I wanted, since I still want to hide opDispatch. Oh well.. :)
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply