# Sealed class type inference with non-exhaustive \`when\`

**URL:** <https://discuss.kotlinlang.org/t/sealed-class-type-inference-with-non-exhaustive-when/1933>\
**Category:** Language Design\
**Created:** [August 23, 2016, 11:34pm UTC](https://discuss.kotlinlang.org/t/sealed-class-type-inference-with-non-exhaustive-when/1933 "2016-08-23T23:34:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshstewart](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/joshstewart/32/2083_2.png) [@joshstewart](https://discuss.kotlinlang.org/u/joshstewart)\
**Post date:** [August 23, 2016, 11:34pm UTC](https://discuss.kotlinlang.org/t/sealed-class-type-inference-with-non-exhaustive-when/1933/1 "2016-08-23T23:34:41Z")

</div>

I have a class defined like this:

```
sealed class Animal {
  class Cat(val isFluffy: Boolean): Animal()
  class Dog: Animal()
}

```

And a test function like this, which doesn’t currently compile:

```
fun isFluffyCat(someAnimal: Animal): Boolean {
  when (someAnimal) {
    is Animal.Dog -> return false
  }
 return someAnimal.isFluffy
}

```

However, if I change it to this, it compiles:

```
fun isFluffyCat(someAnimal: Animal): Boolean {
  when (someAnimal) {
    is Animal.Dog -> return false
    is Animal.Cat -> {} // no-op, just here to satisfy the compiler
  }
  return someAnimal.isFluffy
}

```

It seems like it should be possible for the compiler to infer type on a nearly-exhaustive `when` clause if all other types trigger a return or throw an exception. Is this a change planned for a future release?

---

<div class="post-metadata">

**Author:** ![jacobz\_20](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jacobz_20/32/1468_2.png) [@jacobz\_20](https://discuss.kotlinlang.org/u/jacobz_20)\
**Post date:** [August 28, 2016, 11:18pm UTC](https://discuss.kotlinlang.org/t/sealed-class-type-inference-with-non-exhaustive-when/1933/2 "2016-08-28T23:18:33Z")

</div>

Yeah. I was under the impression (thought I can’t say for certain that I’ve read it before) that when `when` is used as a statement instead of an expression, it shouldn’t be required to be exhaustive.

I must say, though, that I’d redo your function like this:

```
fun isFluffyCat(someAnimal: Animal): Boolean =
  when(someAnimal) {
    is Animal.Cat -> someAnimal.isFluffy
    else -> false
  }

```

That last one allows you to add other animals without needing to update it. Plus, it uses the function expression format, which uses `when` as an expression also.
