Wrapper Struct

Salih Dincer salihdb at hotmail.com
Fri Dec 23 04:26:51 UTC 2022


On Wednesday, 21 December 2022 at 15:02:42 UTC, rikki cattermole 
wrote:
> This may be what you want: 
> https://dlang.org/phobos/std_typecons.html#Proxy

Thank you, now no need proxy! It's works:

```d
void main()
{
   import std.stdio;

   auto p1 = Point(1, 2);
   auto p2 = Point(10, 20);
   writeln(p1 + p2);

   class Foo
   {
     NP!Point point;
     this(double x = 0.0, double y = 0.0)
     {
       point = Point(x, y);
     }
   }

   auto foo = new Foo(0.1234, 567.89);
   writeln(foo.point + p2);
}

struct NP(T) { // NP: No Proxy
   private T value;
   this(T x)
   {
     value = x;
   }

   alias opCall this;
   @property opCall() inout
   {
     return value;
   }

   @property opCall(T x)
   {
     return value = x;
   }
}

struct Point
{
   double x, y;
   pure const nothrow @safe:

   auto opBinary(string op)(in Point rhs) @nogc
   {
     return Point(mixin("x" ~ op ~ "rhs.x"),
                  mixin("y" ~ op ~ "rhs.y"));
   }

   auto opBinary(string op)(int rhs) @nogc
   {
     return Point(mixin("x" ~ op ~ "rhs"),
                  mixin("y" ~ op ~ "rhs"));
   }
}
```


More information about the Digitalmars-d mailing list