Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit operator order within enum affects casting behavior #2151

Open
Grimelios opened this issue Feb 18, 2025 · 0 comments
Open

Implicit operator order within enum affects casting behavior #2151

Grimelios opened this issue Feb 18, 2025 · 0 comments

Comments

@Grimelios
Copy link

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 #2150. The code above is identical to that issue except for two changes:

  1. The explicit cast uses uint8, not uint.
  2. There's a new public static implicit operator uint(Self self) => self; in MyEnum.

This code compiles successfully. However, swapping the order of the two implicit operators (i.e. int first, then uint) reintroduces the same issue as #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant