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

The intention behind expr!! is (or should be)…

I assume that expr is never null. If it is, it means that an internal assumption was broken. The code should crash, but I don’t even blame the caller.

In the Java ecosystem, NullPointerException is sometimes used to say…

The precondition of this method was broken (as a parameter expected to be non-null is null). I blame the caller.

If you want (or are forced to) follow this convention in Kotlin, you should use an explicit throw combined with an if, or ?: (elvis) if applicable.

In this specific class, the intention is actually… I don’t know what’s the intention. Why is defaultCalculator a nullable type in the first place? What are the scenarios where it’s reasonable to instantiate NonNullValuesMap without defaultCalculator?

1 Like