How to alias
Salih Dincer
salihdb at hotmail.com
Thu Jan 20 13:51:02 UTC 2022
On Friday, 14 January 2022 at 17:48:41 UTC, kyle wrote:
>```d
> void main()
> {
> import std.stdio;
>
> Broke foo = Broke(10);
> Broke bar = Broke(20);
>
> writeln(foo + 15); //prints 25 as expected
> writeln(foo + bar); //prints 20
> }
> ```
I guess what you want to do is something like this:
```d
struct Broke
{
double num;
Broke opBinary(string op)(Broke rhs)
if(op == "+")
{
return Broke(this.num + rhs.num);/*
this.num += rhs.num;
return this;//*option 2*/
}
Broke opBinary(string op)(double rhs)
if(op == "+")
{
return Broke(this.num + rhs);/*
this.num += rhs;
return this;//*option 2*/
}
}
void main()
{
import std.stdio;
Broke foo = Broke(5);
Broke bar = Broke(15);
bar = foo + bar; // #1 bar == Broke(20)
writeln(foo + 15); // #2 print ok => 20
writeln(foo + bar); // #3 print ok => 25
}
```
So you don't need to use alias. If you want it the other way
(option 2), turn off option 1 then option 2 will open
automatically. Thus, same Broke() gets values: 20, 35 and 55.
regards,
- Salih
More information about the Digitalmars-d-learn
mailing list