# Add non-null min() function for number arrays

**URL:** <https://discuss.kotlinlang.org/t/add-non-null-min-function-for-number-arrays/23390>\
**Category:** Language Design\
**Created:** [November 4, 2021, 11:13pm UTC](https://discuss.kotlinlang.org/t/add-non-null-min-function-for-number-arrays/23390 "2021-11-04T23:13:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chaoz](https://avatars.discourse-cdn.com/v4/letter/c/ccd318/32.png) [@Chaoz](https://discuss.kotlinlang.org/u/Chaoz)\
**Post date:** [November 4, 2021, 11:13pm UTC](https://discuss.kotlinlang.org/t/add-non-null-min-function-for-number-arrays/23390/1 "2021-11-04T23:13:57Z")

</div>

Right now, to get the minimum (or maximum) of an IntArray, you can use `arr.minOrNull()` which, as the name suggests, can return null (if the array has no elements). However, when we are certain an array is not null, getting the minimum looks kinda ugly:

```auto
val min = arr.minOrNull()!!;

```

It would be way nicer to have a `min()` function that throws if the array is empty. Not having this is also inconsistent with the fact that there is no `averageOrNull()` function, but there is the `average()` function which does indeed throw an exception if the array is null.

Now, there is a `min()` function (which is not only deprecated, but using it throws an actual compile error if used, which I think is a neat feature) which also returns a nullable type. Are there already some plans of replacing this with a non-null returning min function?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [November 4, 2021, 11:34pm UTC](https://discuss.kotlinlang.org/t/add-non-null-min-function-for-number-arrays/23390/2 "2021-11-04T23:34:57Z")

</div>

It is still far from perfect, but alternatively you can use: `minOf { it }`. At least it doesn’t mention anything about nulls and it throws `NoSuchElementException` instead of NPE.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 5, 2021, 1:49am UTC](https://discuss.kotlinlang.org/t/add-non-null-min-function-for-number-arrays/23390/3 "2021-11-05T01:49:00Z")

</div>

> [@Chaoz](#):
>
> Are there already some plans of replacing this with a non-null returning min function?

Yes, we plan to reintroduce `min` with non-nullable return type in Kotlin 1.7.
