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
Enum#each
ignores unnamed members
#13160
Comments
One problem is @[Flags]
enum Foo
X = 0b011
Y = 0b110
end
Foo::X.includes?(Foo::Y) # => true
Foo::Y.includes?(Foo::X) # => true
Foo::X.includes?(Foo::Y | Foo.new(0b1000)) # => true An |
Oh, yeah that's surely another bug in |
In my opinion when you use values that don't have a named member the behavior should be unspecified. So each is behaving correctly. |
But why? That seems unnecessarily restrictive and reduces a lot of flexibility that helps overcome some of the hurdles with being unable to reopen enums. Thinking this forward, I suppose instead of making this unspecified behaviour it might be better to make it an error to instantiate an enum with undefined value if you want to go this route. In my understanding a flags enum is essentially a bit array. Some bit positions are named, others are not. It's always clear which flags are set and which are not. The only difference between a named member and an unnamed one is the name itself. But there are no inherently attached semantics. |
Right, I think it should be an error to create an enum value with a number that doesn't correspond to a member. We copied new from C# but it was probably not a good idea. Enums are closed for a reason: so you can control the behavior. If you want an open enum you probably want something else. |
Actually, nevermind. Ignore me. |
Enum#each
only iterates named enum members and ignores any additional flags created from an explicit integer value.This is unexpected and confusing which can lead to bugs (cf. #13155).
This behaviour is in conflict with
Enum#includes?
which affirms the presence of unnamed flags.Enum
does not explicitly implement theEnumerable
interface, but it should be expected that#each
and#includes?
agree on what's part of the collection.Example:
Enum#each
should iterate the named members and also any remainder value, split into individual flags. In the example above(Foo::Baz | Flags.new(4) | Flags.new(8))#each
would iterate members with values 2, 4 and 8.The implementation could be similar to #13155.
The text was updated successfully, but these errors were encountered: