Suppose I have the following:
fun makeSound(val animal: Animal) = when(animal) {
is Lion -> animal.roar()
is TRex -> animal.roar()
is Cow -> animal.moo()
}
Usually I would simplify this by simply adding a RoaringAnimal
interface and asking is RoaringAnimal
. But is there another way of combining multiple is
clauses into one?
is Lion
andis TRex
here? Isn't it already easy enough to just use it the way you show? Otherwise I would rather update your hierarchy if possible... (side-note:val
is probably wrong here and=
requires an exhaustivewhen
)