If you're trying to optimize an enum's size it can be nice to use different variants instead of booleans. But it comes with a tradeoff: code reuse.
So you have this large enum with things like StringKey
and NonValidatingStringKey
and you have to copy-paste the code for StringKey
into the NonValidatingStringKey
match case because they do slightly different things, e.g.
match foo {
StringKey(x) => {
//code here
return option.map(Some).ok_or_else(|| ValidationError(etc, etc))
},
NonValidatingStringKey(x) => {
//pretty much exact same code here
return Ok(option)
}
}
It'd be nice if you could do something like this:
match foo {
StringKey(x) as let skip=false | NonValidatingStringKey(x) as let skip=true => {
//code here
if skip {
return Ok(option)
} else {
return option.map(Some).ok_or_else(|| ValidationError(etc, etc))
}
}
}
So you get all of the benefits of fallthrough (aka code reuse) with none of the drawbacks (because it's not fallthrough).