Like the comment about default lifetime parameters, this is something I'd like to see happen, but won't push forward.
The one way in which scoped enum variants is quite annoying is when you are matching on them:
match x {
Foo::Bar => ...
Foo::Baz => ...
Foo::Quux => ...
}
Especially if the type name is long, it can get quite annoying. There have been various proposals to fix this (e.g. allowing _::Bar
), here is mine.
For patterns, variants of the type of the scrutinee (I don't love this term!) should be brought into scope. This is basic typed based resolution, just like how methods on the type are in scope when the .
operator is applied to a value. Nothing particularly magic here. Now, its very easy, you can just write variants:
match x {
Bar => ...
Baz => ...
Quux => ...
}
However, this would be a breaking change because you could have used a nullary variant as an ident today, intending to match all patterns with that name. This would be a very poor choice, but it's allowed.
So I would propose to make this change at an edition boundary, with rust fix changing any code which uses a variant name as a binding pattern to change it to Bar @ _
, in which Bar
is therefore unambiguously a binding.
I read the discussion on namespacing enums last night (many users probably don't know that enum variants were not namespaced at all until shortly before 1.0), and this possibility was brought up very early on. It was even pointed out that this is how Java enums work (they are namespaced, but not in switch statements). In the 5 and a half years since that discussion (my god!), no additional issue with this way of doing things other than the incompatibility with very strange code has arisen, as far as I know.