No need opUnary
j
my.email at gmail.com
Sun Dec 18 21:17:02 UTC 2022
On Sunday, 18 December 2022 at 16:21:05 UTC, Salih Dincer wrote:
> 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
Why are you using @property everywhere?
More information about the Digitalmars-d-learn
mailing list