# Null Type Checking with Elvis Return

**URL:** <https://discuss.kotlinlang.org/t/null-type-checking-with-elvis-return/27373>\
**Category:** Support\
**Created:** [September 11, 2023, 4:46pm UTC](https://discuss.kotlinlang.org/t/null-type-checking-with-elvis-return/27373 "2023-09-11T16:46:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gchallen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gchallen/32/6915_2.png) [@gchallen](https://discuss.kotlinlang.org/u/gchallen)\
**Post date:** [September 11, 2023, 4:46pm UTC](https://discuss.kotlinlang.org/t/null-type-checking-with-elvis-return/27373/1 "2023-09-11T16:46:16Z")

</div>

A student in my CS1 course asked about the following example:

```kotlin
fun test(values: IntArray?): Int? {
   if (values?.isEmpty() ?: true) {
      return null
   }
   return values.min()
}

```

As shown when the code is loaded into the [Kotlin Playground](https://pl.kotl.in/vRYWW-FnP), the compiler errors here on the final line, claiming that `values` is still nullable. But it doesn’t seem like it can be, since `null` `values` will end up in the right side of the Elvis operator, entering the `if` and triggering the early return.

Rewrites of the code work as expected, such as:

```kotlin
fun test(values: IntArray?): Int? {
   if (values == null || values.isEmpty()) {
      return null
   }
   return values.min()
}

```

Curious if this is just a corner case in Kotlin’s `null` tracking or if we’re missing something more subtle here…

---

<div class="post-metadata">

**Author:** ![frozenice](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/frozenice/32/7436_2.png) [@frozenice](https://discuss.kotlinlang.org/u/frozenice)\
**Post date:** [September 15, 2023, 6:29pm UTC](https://discuss.kotlinlang.org/t/null-type-checking-with-elvis-return/27373/2 "2023-09-15T18:29:11Z")

</div>

Putting this into IntelliJ yields a weak warning:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/8/83f5e731840dfcdafd4e78bfc529a67a67d6407f.png)

> Equality check should be used instead of elvis for nullable boolean check

If we accept that refactoring, the code is changed to:

```kotlin
fun test(values: IntArray?): Int? {
  if (values?.isEmpty() != false) {
    return null
  }

  return values.min()
}

```

`values` is now properly smart casted and the error is no more:

![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/c/c82c9b8e69620947203da649d9313485688569bc.png)

As to why that works and the original does not: I guess the compiler just isn’t that smart here. You could try searching the [issue tracker](https://youtrack.jetbrains.com/issues/kt) or create an issue there about this.

A note about style: both variants can be confusing to readers - there was also some discussion in [an old thread](https://discuss.kotlinlang.org/t/is-the-idiom-for-checking-nullable-booleans-helpful/8238) about this. To make it clearer, you can replace the if block with

```kotlin
  if (values == null || values.isEmpty()) {
    return null
  }

```

which makes the intention immediately obvious and also produces the correct smart cast. To tidy this up a bit, you could also write an extension function `isNullOrEmpty` (see above mentioned thread for an example), so the code would become

```kotlin
  if (values.isNullOrEmpty()) {
    return null
  }

```

which couldn’t be clearer, really (I would probably do this).

And as a final remark: you can just use `values?.minOrNull()` ([ref](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/min-or-null.html)) if that’s all `test` does, no need to reinvent it. 🙂

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [September 16, 2023, 9:39pm UTC](https://discuss.kotlinlang.org/t/null-type-checking-with-elvis-return/27373/3 "2023-09-16T21:39:22Z")

</div>

I must admit, I’ve never quite understood the reason for that warning.

The equality check is a little more concise — but to me, it’s a little harder to see the ‘default’ truth value it assumes when there’s a null, something that’s much more obvious in the elvis version.

(But yes, I’ve added a few methods of my own along the lines of `isNotNullOrEmpty()`, which are concise, simple, _and_ clear!)
