-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add support for enum variants scoped under their type (enum mod
)
#10090
Comments
I'm a big fan of this. It'd be nice if the enum type name wasn't required in a match statement over a variable of the type, i.e. pub enum mod Speed {
Fast,
Slow
}
let x = Speed::Fast;
match x {
Fast => {}
Slow => {}
} As opposed to match x {
Speed::Fast => {}
Speed::Slow => {}
} |
@sfackler: I updated the Issue text with an example showing how that would be doable with the existing scoping primitives. |
I really like this idea. |
I quite like this. I assume that the final code example in "The possible solution" was meant to say |
@kballard Indeed, I forgot the |
Yes please. Servo's style system has tons of deeply nested modules that would become unnecessary with |
+1 here. Both styles - ie. |
If we allow |
So I was thinking "can't this just be a macro?" and the answer is "not quite" -- in particular, the type of the enum would have to be |
@nikomatsakis said:
Do they really? The working example in the initial message seems to use |
@SimonSapin Something like this will fail: pub use Thing::Thing;
pub mod Thing {
pub enum Thing {}
}
pub fn do_a_thing(thing: Thing) {}
@nikomatsakis My biggest problem with a macro-based solution is that it makes generated documentation enormously confusing. If I see a For example, here's rustdoc that uses a normal enum: http://sfackler.com/doc/enum/ and here's rustdoc for the same setup, but using the current workaround enum mod solution: http://sfackler.com/doc/enum-mod/ |
Oops, I was using a reexport above instead of a type alias. The http://sfackler.com/doc/enum-mod/ example is updated. |
The consensus on this at today's meeting is that this feature should not be added to the language now. We may want to revisit this later (post-1.0), but for now we have decided to leave this out of 1.0 |
Look what just happened! #18973 |
Current situation
Right now, enum types and their variants are scoped at the same level, which means this:
leads to these paths:
We will likely not change this, because it often makes working with enums easier, and because many languages with similar enum types work that way.
The problem
However, the restriction to a flat hierarchy currently leads to a lot of variants getting their type name embedded in the variant name to avoid collisions and/or ambiguity. For example, a random enum picked out of libsyntax reads like this:
The possible solution
To improve this situation, it would be convenient to have a enum declaration syntax for scoping the variants under their type instead, like this:
The above example could then get rewritten to
and get used as
Sigil::Borrowed
,Sigil::Owned
etc, or directly imported in the local scope if there is no collision.Emulating it
You can currently kinda emulate it with
but this is very verbose, complicated, and creates more definitions and moving parts than necessary.
Working example
This uses the workaround above to show how using such a scoped enum would look like in practice.
The text was updated successfully, but these errors were encountered: