April 08, 2017 overring binary | ||||
---|---|---|---|---|
| ||||
I have a custom type and I'm trying to do things like x~1 and 1~x. I can get x~1 no problem by overriding the op "~" but how to I get 1~x to convert 1 in to typeof(x)? instead of x in to typeof(1) so to speak? I really want D to realize that 1~x is suppose to use ~ of x, not 1. |
April 08, 2017 Re: overring binary | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jethro | On 08/04/2017 7:46 AM, Jethro wrote:
> I have a custom type and I'm trying to do things like
>
> x~1 and 1~x.
>
> I can get x~1 no problem by overriding the op "~" but how to I get 1~x
> to convert 1 in to typeof(x)? instead of x in to typeof(1) so to speak?
>
> I really want D to realize that 1~x is suppose to use ~ of x, not 1.
struct Foo {
int x;
Foo opBinaryRight(string op)(int y) {
static if (op == "~") {
return Foo(x + y);
} else static assert(0, "not implemented");
}
}
void main() {
import std.stdio;
writeln(1 ~ Foo(2));
}
|
Copyright © 1999-2021 by the D Language Foundation