How am I supposed to get rid of the null-ability of function value?

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 ?

val d = defaultCalculator ?: throw Error()
val generated = d(key)
1 Like

There are a few problems with your code, where I don’t understand why you chose to do it this way.
First

if(defaultCalculator == null)
     return null!!

looking at documentation of !! you see that it throws an exception if the value is null so this code is the same as

if(defaultCalculator == null) throw NullPointerException()

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.

Thanks for all the notes.
It works for me with compiled kotlin too, but it was not (and still isn’t) working within a kotlin scratch file.

Now that you pointed it out - I find null!! a delightful shortcut to throw NullPointerException() :wink: