# Math operators with nulls

**URL:** <https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159>\
**Category:** Uncategorized\
**Created:** [December 6, 2020, 1:54pm UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159 "2020-12-06T13:54:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![labai](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@labai](https://discuss.kotlinlang.org/u/labai)\
**Post date:** [December 6, 2020, 1:54pm UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159/1 "2020-12-06T13:54:45Z")

</div>

Are there some fundamental restrictions for operators in kotlin, that they don’t support nulls?  
It seems quite intuitively for me, if one of arguments is null, result become null.

I want to create own class for decimals [Deci.kt](https://github.com/labai/la-utils/tree/main/deci), which would solve for me division operator (scale, rounding HALF\_UP).  
In addition I am thinking to allow to use nulls, but I am wondering, why kotlin doesn’t support such thing for other operators.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [December 6, 2020, 2:08pm UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159/2 "2020-12-06T14:08:18Z")

</div>

My personal experience is (and I think many will agree with that) that `null` values in math environments often mean that you have an error somewhere else in your code. In that case you want to detect that error as soon as possible.  
That said kotlin is a null safe language so maybe that is an outdated view of things and could be changed.  
In any case you can always create your own operators that work on nullable values using extension functions, eg:

```auto
operator fun Int?.plus(other: Int?): Int? if(this == null || other == null) null else this!! + other!!

```

You could also create an issue at [https://kotl.in/issue](https://kotl.in/issue) to get those operators added to the stdlib. Not sure if they will be. If you do create an issue make sure to post a link here so people can find it.

---

<div class="post-metadata">

**Author:** ![labai](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@labai](https://discuss.kotlinlang.org/u/labai)\
**Post date:** [December 6, 2020, 3:41pm UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159/3 "2020-12-06T15:41:52Z")

</div>

Ok, I’ll take this point of view.  
Actually, nullable operators aren’t biggest issue with BigDecimals anyway.

---

<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:** [December 7, 2020, 2:51am UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159/4 "2020-12-07T02:51:47Z")

</div>

The problem I see is that if operators all handled nulls silently, then there would be a real risk of forgetting about them.

We see this in Java, where the compiler doesn’t do anything to stop you trying to handle a potential null in an unsafe way.⠀(The resulting NullPointerException is probably the most common exception, showing just how badly this works.)

Kotlin improves on this _not_ by making every access safe, but by giving a compile-time error on unsafe access (and by giving you tools to easily handle potential nulls in various ways).

Having the compiler give an error when doing something unsafe with a nullable value is useful: it forces you to think, to consider what a null would mean at that point, and how it should be handled.

And no, simply propagating the null is often _not_ the right thing to do.⠀A null could mean many things: ‘not initialised yet’, ‘not provided’, ‘not overridden’, ‘failed’, ‘not present’, ‘empty’, ‘not authorised’, or a variety of other subtly-different things.⠀Sometimes you should substitute a default value, or avoid the whole block, or perform a calculation or DB fetch, or treat it as ‘all’ or ‘none’, or raise an error, or add a new item, or various other actions.

Kotlin is great at focusing your attention on the things that need it, and smoothing away the things that don’t.⠀I’d suggest that nulls fall into the first category.

---

<div class="post-metadata">

**Author:** ![Beholder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/beholder/32/2078_2.png) [@Beholder](https://discuss.kotlinlang.org/u/Beholder)\
**Post date:** [December 8, 2020, 7:27pm UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159/5 "2020-12-08T19:27:14Z")

</div>

> [@labai](#):
>
> It seems quite intuitively for me, if one of arguments is null, result become null.

It’s NOT intuitive. This depends heavily on the context. It may be null, may be infinity, may be uncertainty, may be undefined behavior, may be just error.

---

<div class="post-metadata">

**Author:** ![labai](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@labai](https://discuss.kotlinlang.org/u/labai)\
**Post date:** [December 12, 2020, 8:56am UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159/6 "2020-12-12T08:56:53Z")

</div>

I would agree that operations with nulls makes little sense. As most of things with nulls.  
But… Kotlin, as a language, allows to use nulls and provides nullable types for that. And Kotlin supports easy usage of nullables (elvis and etc) in other places.

So, even if most of things looks better w/o nulls, but in real life they still are used. An example, sometimes in practice we have mutable object with nullable fields (e.g. jpa entity) and smart cast doesn’t work for them.  
Usually we need to add “!!” in formula for those fields in each place.  
I would say it isn’t a big problem, as they do not pollute a code, but still it is a philosophical question, should language forbid nullables in formulas, while they are allowed in other places.

And, btw, yes, I usually try to use non-nullable variables where is possible, e.g. using no-arg-constructor plugin.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [December 12, 2020, 11:15am UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159/7 "2020-12-12T11:15:01Z")

</div>

> [@labai](#):
>
> but still it is a philosophical question, should language forbid nullables in formulas, while they are allowed in other places.

Kotlin doesn’t forbid nullable values in forumlars though. It’s just currently not allowed by the functions in the std-lib. Both @Beholder and @gidds raised a good point, that even in math enviornment `null` can have very different meanings and shouldn’t automatically be propogated. If this is different in your library you can always add extension methods to allow nullable values.

---

<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:** [December 13, 2020, 1:37am UTC](https://discuss.kotlinlang.org/t/math-operators-with-nulls/20159/8 "2020-12-13T01:37:21Z")

</div>

> [@labai](#):
>
> smart cast doesn’t work for them.  
> Usually we need to add “!!” in formula for those fields in each place.

If the compiler won’t smart-cast a field, then there are very probably conditions in which it could be null, and so `!!` could throw a NullPointerException. If you’re not prepared to handle that, then you should probably look at something other than `!!`.

Yes, there _are_ some situations in which you really do know something the compiler doesn’t. But in most of those cases, it’s better to _tell_ the compiler — e.g. by using an immutable value or `let` or a temporary variable — than to fight it. Even if you can see that `!!` should never fail, it’s easy for a future code change to introduce that possibility without realising the problem.
