No need opUnary

Salih Dincer salihdb at hotmail.com
Sun Dec 18 16:21:05 UTC 2022


Don't you think it's interesting that it doesn't need unary 
operator overloading?

```d
import std.stdio;

struct S
{
   int value;

   alias opCall this;
   this(int i) {
     value = i;
   }

   alias opAssign = opCall;
   @property opCall(int x) {
     return value = x;
   }

   @property opCall() inout {
     return value;
   }

   @property opOpAssign(string op)(int x) {
     write(":"); // came here before
     mixin("return value"~op~"=x;");
   }
   // no need: opUnary(string op)();
}

void main()
{

   S a = S(10),
     b = S(-1);

   writeln(a + b); // 9
   writeln(++a + b); // :10

   a += 10; // :

   assert(a == 21);
   writeln("\n--");

   writeln(-b); // 1
}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list