https://kotlinlang.org logo
Title
a

arekolek

03/06/2019, 9:25 AM
fun foo(bar: List<Int>): Int {
    check(bar.isNotEmpty())

    return bar.max()!! / 2
}
How would you deal with
max
returning a nullable value, other than
!!
? Does it bother you?
w

wbertan

03/06/2019, 9:29 AM
If
foo
is fine to thrown exception (as I suspect based on the
check
), you can do something like:
fun foo(bar: List<Int>): Int =
        bar
                .max()
                ?.let { it / 2 }
                ?: throw Exception("foo failed")
m

Marc Knaup

03/06/2019, 9:50 AM
You can use
error("foo failed")
instead of
throw Exception
👍 1
p

Pavlo Liapota

03/06/2019, 2:03 PM
I have written
.maxOrThrow()
extension function
👍 1
o

oday

03/14/2019, 1:31 PM
^^
steal from him