https://kotlinlang.org logo
Title
i

igorvd

06/28/2018, 12:55 PM
is there an away to avoid the assertion operator to make the value type not null on the following case?
if (key in map) {
        val value = map[key]!!
    }
If I remove the assertion operator, the type of the
value
will be
?
p

Pavlo Liapota

06/28/2018, 5:16 PM
if (key in map) {
    val value = map.getValue(key)
}
or
val value = map[key]
if (value != null) {
    // ...
}
i

igorvd

06/28/2018, 5:33 PM
Cool, I didn't knew about the
getValue
method. It works like a charm here. Thanks 😃
g

gildor

06/30/2018, 1:39 PM
Also to make it more explicit with custom error message:
val value = map[key] ?: error("My custom error")