8

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?

1
  • why would you want something that combines is Lion and is 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 exhaustive when)
    – Roland
    Nov 16, 2018 at 16:34

4 Answers 4

7

Normally you can combine the clauses as shown in Yoni's answer.

But in the specific case that roar is defined on Lion and TRex, but not on Animal, you can't.

This is because the compiler inserts a smart cast:

is Lion -> animal.roar()

is really

is Lion -> (animal as Lion).roar()

but in is Lion, is TRex -> clause, it wouldn't know what cast to insert.

In principle the compiler could be extended to handle such cases by inserting another when:

is Lion, is TRex -> animal.roar()

would become

is Lion, is TRex -> when(animal) {
    is Lion -> animal.roar() // works as before
    is TRex -> animal.roar()
}

but I wouldn't expect this to happen

1
  • "In principle the compiler could be extended to handle such cases .." Yup, and actually this was exactly what I thought was the behavior. Thanks for the explanation about the compiler not knowing which smart cast to insert. Nov 22, 2018 at 8:54
7

UPDATE: the answer below was written before the question specified that roar was a method on the animal parameter. As the question now stands, the answer below will no longer work, however it still shows how multiple conditions can be combined in one line in a when statement.

You can combine them:

fun makeSound(animal: Animal) = when(animal) {
  is Lion, is TRex -> roar()
  is Cow -> moo()
}
4
  • How will this work? Will this not just catch a generic Animal type? Nov 16, 2018 at 13:06
  • roar will only be called if you pass in a Lion or TRex. What do you mean by "catch" exactly? I wonder if you need to slightly clarify your question?
    – Yoni Gibbs
    Nov 16, 2018 at 13:07
  • Ah, yes. I was not precise in my question. I modified it now. Nov 16, 2018 at 13:16
  • As Alexey says, now you've updated your question to specify that roar is a method on Lion and on TRex, then no, there is no (nice) way of doing this without a new interface or base class. (You could use Reflection but that wouldn't be a good idea here.)
    – Yoni Gibbs
    Nov 16, 2018 at 13:22
3
fun makeSound(val animal: Animal) = when(animal) {
  Lion, TRex -> animal.roar()
  Cow -> animal.moo()
}

You don't need 'is'. Just have it comma separator which works as OR here.

-2

What you are doing here looks like a code smell to me. Why don't you use polymorphism for this type of problem?

interface Animal {
    fun makeSound()
}
class Lion : Animal {
    override fun makeSound() { println("roar") }
}
class Trex : Animal {
    override fun makeSound() { println("ROAAAARRRR") }
}
class Cow : Animal {
    override fun makeSound() { println("moo") }
}

fun makeSound(animal: Animal) {
    animal.makeSound()
    // due tue "late binding", this will automatically call the correct method - no need to check what kind of animal it is! 
}
1
  • Like I said in the question, using a interface is absolutely possible. However, the question is not one of best practice but rather what is possible in Kotlin. Nov 18, 2018 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.