Norbi
09/16/2021, 5:17 PMfun <T : Any> isNotNull(value: T?): Boolean {
contract {
returns(true) implies (value != null)
returns(false) implies (value == null)
}
return value != null
}
class ContractTest {
@Test
fun test1() {
val a: String? = null
if (isNotNull(a)) {
println(a.uppercase()) // Works as expected: "Smart cast to kotlin.String"
}
val notNull = isNotNull(a)
if (notNull) {
println(a.uppercase()) // Compilation error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?"
}
}
}
ephemient
09/16/2021, 5:34 PM