Preconditions should activate smart cast, no?


#1

Consider the following example:

fun test(s: String?): String {
    require(s != null)
    return s!!  // null check is still required here
}

shouldn’t s be smart-casted after the precondition check?


#2

Require is a regular function so the compiler doesn’t “know” that it throws on a false parameter. You will have to wait for the contract work to be complete.


#3

More details about that here: Status of kotlin.internal.contracts?