If one wants to work with enums, one either has to write out the enum name repeatedly or use a use statement. I think it would be nice to have an alternative syntax.
Let's say that I have an enum
enum Value {
a = 1,
b = 2
}
Now if Value implements e.g. std::ops::Add, one has to write Value::a + Value::b or use Value::*; a + b.
I'd like to propose an alternative syntax, Value::{a + b}. It's much more understandable than using use and also short.
This syntax would really shine when working with bitflags. E.g. instead of
This seems to me like an unusual special case, and I'm not sure if it's worth the language syntax overhead compared to, for instance, a macro: foo!(wgpu::BufferUsage, MAP_READ | COPY_DST | COPY_SRC). Or compared to { use wgpu::BufferUsage::*; MAP_READ | COPY_DST | COPY_SRC }.
I certainly see the use case, and I'm not suggesting it couldn't be made cleaner. But I'm not sure that it's something we should add special syntax to the language for.
In practice, I think what would actually be much nicer would be "shortcut" sugar similar to how Swift does it.
enum Foo {
case bar, baz
}
function takesFoo(_ foo: Foo) { /* ... */ }
// Both of these are allowed
takesFoo(Foo.bar);
takesFoo(.bar);
This works in general in Swift anywhere that the appropriate type can be inferred by the compiler, and it makes APIs in Swift pleasantly briefer without loss of explicitness or clarity in my experience.
(I say this as someone who in general finds Swift to be substantially more syntax-sugary than I prefer; this is one of the only bits of Swift sugar I really miss when working with Rust.)
Edit to add: I don't mean that the way it would look if Rust adopted a similar idea would be the same as in Swift, just that a general feature that supported that kind of elision would eliminate this particular pain point and could work nicely in the language in general. We'd probably have :: or something instead.
I believe the Rust way to spell this would be <_>::Bar. Unfortunately, I don't believe this is inferred quite as aggressively as in Swift, so this example does not currently work. I also think it's unreasonably sigiltastic...
I think the right answer is roughly what Josh already said.
I've seen proposals for both enum::Stuff and _::Stuff before, though I don't remember where the threads are. I don't think they got far enough to get written up in RFC format.
Same. In skimming through the other discussions, it seems like an improved proposal could still work. And I'd be very curious to see what the constraints would be on our ability to make a general form of it, usable with methods as well.
I don't see the problem with the currently-available use MyEnum::*;-based solution? When it is put in the body of a method or fn that uses MyEnum variants extensively, to me it seems like the extra one liner is acceptable syntactic overhead that doesn't even pollute the namespace outside the method/fn in question.
What I'm saying is, you're very specifically trying to make it easier to use binary operators on enums, and I honestly cannot even remember the last time I've used a binary operator on an enum.