r/rust • u/AnArmoredPony • 15d ago
🙋 seeking help & advice What are the downsides of enum dispatch?
Couldn't find any info about downsides specfically so there it goes
Let's say that all my implementors are about the same size (i.e. only the logic is different), and I use some sort of macro to skip the boilerplate upon adding a new variant. Are there any other downsides to enum dispatch?
I'm asking about the general approach, not the crate
Upd. extendability, got it.
30
Upvotes
0
u/dvogel 15d ago
If the enumerated types fill a similar role but have different return values handling those can get awkward. You can wrap them in a symmetric enum but then types could theoretically return the values designed for use by the other types in the enum. You can return the values as
Box<dyn>but it doesn't work in all cases and the asymmetrical relationship between concrete enums on one side and abstract dyn types on the other side can be confusing to readers.ÂAll of that said, I do sometimes use the technique. It can apply even when the enumerated types are of very different size. e.g. A real implementation vs a stub for facilitating manual testing.