Why doesn’t this work ```@OptIn(ExperimentalContra...
# random
i
Why doesn’t this work
Copy code
@OptIn(ExperimentalContracts::class)
private fun <T : Any> T?.isNotNull(): Boolean {
    contract {
        returns(true) implies (this@isNotNull != null)
    }
    return this != null
}
d
I don't think contracts are preserved with variables like that.
Does it work when you do
!= null
?
i
Copy code
paymentRule ?: return
works
Copy code
check(paymentRule != null)
works After all of they payment rule is not null in scope
But i want reach something different
i want boolean, that means, that value is null
d
The first two are easy for the compiler.
The compiler can't do that with the boolean you're trying to use.,
s/can't/doesn't/
i
That is purpose of contracts, is not it?
d
I suppose so but contracts are not in their final form yet.
k
Isn't this exactly the same reason foo compiles but bar doesn't? Or are contracts meant to do something a bit more magical that that?
Copy code
fun foo(n: Int?) {
    if (n != null) {
        val m: Int = n
    }
}

fun bar(n: Int?) {
    val b = n != null
    if (b) {
        val m: Int = n
    }
}
☝🏼 1
d
Exactly! That's what I'm trying to say. The compiler drops the contract once the variable
b
is used.