-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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#inspect
should be more specific
#12884
Comments
A more succinct format with square brackets: I think that's better because it conveys the same information with less space. This should also work great as a representation format for unnamed members (cf. #12883) which wouldn't be valid syntax for FlagsEnum::None # => FlagsEnum::None
FlagsEnum::One # => FlagsEnum::One
FlagsEnum.new(128) # => FlagsEnum[128]
FlagsEnum::All # => FlagsEnum[One | Two | Three]
(FlagsEnum::One | FlagsEnum::Two) # => FlagsEnum[One | Two]
(FlagsEnum::One | FlagsEnum.new(128)) # => FlagsEnum[One | 128]
(FlagsEnum::One | FlagsEnum.new(8) | FlagsEnum.new(16)) # => FlagsEnum[One | 24]
(FlagsEnum::One | FlagsEnum::Two | FlagsEnum.new(16)) # => FlagsEnum[One | Two | 16] Footnotes |
I think separating elements with a comma instead of a pipe will allow implementing it as a method instead of a macro. |
@asterite Yes, we could use symbols for autocasting. I considered that as well. But that would potentially be ambiguous. The mapping between member name and symbol is not exactly 1:1. Symbol autocasting is case-insensitive, which would lead to problems when an enum contains members with the same letters but different casing. /ref #11660 enum Foo
Bar
BAR
end
Foo.new(:BAR) # => Bar An unambiguous literal representation of the member name is necessary for I suppose we could use the literal casing for |
Idea: disallow two enum members if their snake case lead to the same value. |
@asterite Sounds good for a regular code, but what about lib bindings where you cannot control the member naming? |
How can you not control that? |
Yeah, I think this should be controllable, though it means deviating from the original spelling of constants. Not sure if that would be a huge problem, but it causes some friction. However, this uniqueness validation would definitely be a breaking change, so we couldn't enforce it short term. So I'd leave that beside for now and just implement a format for |
Oh, well I guess you can reassign values, forgot about that 🤦♂️ |
In any case, it seems that to implement this it will require a macro, and separating values by commas will be easier to implement. |
Right, using a comma for simplicity makes sense. |
An edge case is a flag enum with a composite member: @[Flags]
enum FlagsEnum
One
Two
OneTwo = One | Two
end
FlagsEnum::OneTwo
A common use case for this is the autogenerated |
Enum#inspect
prints the name of the enum member, or the numerical value if it's unnamed. It is the same asEnum#to_s
.This is ambiguous when multiple enums share the same member names.
I think this is okay for
#to_s
but#inspect
should be unambiguous as possible. Using the full class name should help to fix that.Of course this is less succinct, but I think that's acceptable.
#to_s
would be the shorter, but less precise alternative.For flag enum values, the fully qualified name does not need to be repeated for every member. Following the syntax of
Enum.flags
method works great for that:The text was updated successfully, but these errors were encountered: