Reputation: 9018
Is there a way to tell Kotlin compiler that we guarantee map contains a key?
In the example:
val dummyMap = mapOf (
1 to 2,
2 to 3
)
...
someByteArray[some index] = dummyMap[some value that can be only 1 or 2]
Compiler will complain that dummyMap return value can be null
.
I'd like to communicate to compiler I know that key will always be found in the map so it doesn't expect nullable value.
Upvotes: 1
Views: 715
Reputation: 54234
Kotlin Collections already defines the Map.getValue()
extension function, which returns an element with non-nullable type or throws NoSuchElementException
.
Now this will compile fine:
someByteArray[some index] = dummyMap.getValue(1)
Upvotes: 3