Beef
Beef copied to clipboard
Implicit operator order within enum affects casting behavior
public enum MyEnum
{
public static implicit operator uint(Self self) => self;
public static implicit operator int(Self);
case A;
case B;
case C;
}
static
{
public static void Main()
{
MyEnum e = ?;
int i = e; // Works!
uint8 u = (uint8)e; // Also works!
}
}
I discovered this issue while experimenting with https://github.com/beefytech/Beef/issues/2150. The code above is identical to that issue except for two changes:
- The explicit cast uses
uint8, notuint. - There's a new
public static implicit operator uint(Self self) => self;inMyEnum.
This code compiles successfully. However, swapping the order of the two implicit operators (i.e. int first, then uint) reintroduces the same issue as https://github.com/beefytech/Beef/issues/2150 ("Unable to implicitly cast int8 to uint8"). This indicates that declaration affects how operators are resolved. Whether this behavior is "correct" may be up for debate, but I'd argue it's unintuitive.