I have a few instances of this sort of code:
enum class Cat { Fat, Greedy, Sleepy, Lasagna }
...
when (cat) {
Fat -> // do fat
Greedy -> // do greedy
else -> throw RuntimeException("You have a $cat cat - I don't know about those.")
}
I would like it to say "You have a $cat cat - I only know about Fat and Greedy" but I don't want to have to update the message when I add a new clause.
- Is there any way to read the options known by a 'when' clause? (I suspect not, but Kotlin is endlessly surprising.)
I don't want to decorate the enum with flags that need to be kept up to date with the when clause, nor add lambdas to the enum, to turn this when clause into dispatch-based logic.
Thanks.
Aucun commentaire:
Enregistrer un commentaire