How to overload opertators for enums
Tobias Pankrath
tobias at pankrath.net
Tue Jun 26 02:39:19 PDT 2012
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);
More information about the Digitalmars-d-learn
mailing list