This is an argument me and a friend have gotten into several times now with no real resolution, and I’m wanting to get some more opinions on it.
Basically he’s arguing that rust enums aren’t really that useful because they could always be represented using traits, plus they’re effectively a wrapper around a union with a switch statement (and switch statements are evil because they’re hard to maintain and could be done much more nicely with good polymorphism).
On the other hand, I’m a massive fan of using enums to represent things like the different error types (à la Error handling with a custom type from The Book), or using it to represent the different node types in an AST.
Obviously there’s a time and a place where using enums might be a good idea, and others where it’s a horrible idea that’ll just create pain for you down the track. But this is more along the lines of long term development and making sure a single change isn’t going to force you to rewrite hundreds of call sites or match statements.
So I’m wondering:
- What situations would you recommend using an enum in?
- Where would it be a better idea to use a different tool (trait objects, etc)?
- Can/should enums be used as a limited form of polymorphism?
- Can you give examples of some really elegant/horrible uses of enums?
- How are rust-style enums different to/better/worse than C-stype enums?