class NonNullValuesMap<K, V>(private val map: MutableMap<K, V>, private val defaultCalculator: ((K) -> V)? = null)
: MutableMap<K, V> by map {
override operator fun get(key: K): V {
val existing = map[key]
if (existing != null)
return existing
if (defaultCalculator == null)
return null!!
val generated = defaultCalculator!!(key) //<< error here: Expression 'defaultCalculator!!' of type Nothing can not be invoked as a function
map[key] = generated
return generated!!
}
}
How am I supposed to get rid of the null-ability of val defaultCalculator ?
If this is what you want than throw an exception. Don’t use !!.
__
That said I can’t reproduce your compiler error. Your code works fine for me. Normally I would say you can change defaultCalculator!!(key) to defaultCalculator(key) since you did the null check above, but there seems to be a bug in kotlin with generics(https://youtrack.jetbrains.com/issue/KT-29911) so you have to keep the !! for now.