https://kotlinlang.org logo
#feed
Title
# feed
m

muliyul

09/15/2020, 2:12 PM
People tend to forget about extension functions. What do you think about these alternatives:
fun 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)
Copy code
val a: String? = null
val b: String? = ""

if(a.isNull() && b.isNotNull()) {
 ...
}
You can also add contracts to enable smart casts:
Copy code
fun Any?.isNull(): Boolean {
 contract {
        returns(true) implies (this == null)
    }
 return this == null
}
*Note: contracts are experimental.
5 Views