muliyul
09/15/2020, 2:12 PMfun Any?.isNull() = this == null
and
fun Any?.isNotNull() = this != null
.
That way you can assert any nullable type in a more readable way (the Kotlin way IMHO)
val a: String? = null
val b: String? = ""
if(a.isNull() && b.isNotNull()) {
...
}
You can also add contracts to enable smart casts:
fun Any?.isNull(): Boolean {
contract {
returns(true) implies (this == null)
}
return this == null
}
*Note: contracts are experimental.