is there an away to avoid the assertion operator t...
# announcements
i
is there an away to avoid the assertion operator to make the value type not null on the following case?
Copy code
if (key in map) {
        val value = map[key]!!
    }
If I remove the assertion operator, the type of the
value
will be
?
p
Copy code
if (key in map) {
    val value = map.getValue(key)
}
or
Copy code
val value = map[key]
if (value != null) {
    // ...
}
i
Cool, I didn't knew about the
getValue
method. It works like a charm here. Thanks 😃
g
Also to make it more explicit with custom error message:
Copy code
val value = map[key] ?: error("My custom error")