June 26, 2012 How to overload opertators for enums | ||||
---|---|---|---|---|
| ||||
This should be a "tribool"-like type. enum Value : byte { Undef = 0, True = 1, False = -1 }; unittest { with(Value) { assert(~Undef == Undef); // failure assert(~True == False); assert(~False == True); } When I overload opUnary!("~") in this way, unittests fail. Value opUnary(string op)(Value operand) if(op == "~") out(result) { with(Value) assert(result == Undef || result == False || result == True); } body { return cast(Value)(operand * -1); } What is the correct way to do this? This works: assert(Value.Undef.opUnary!"~"() == Value.Undef); |
June 26, 2012 Re: How to overload opertators for enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | Tobias Pankrath:
> When I overload opUnary!("~") in this way, unittests fail.
Currently in D you can only overload struct and class operators.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation