Math operators with nulls

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:

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 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.

1 Like