what about smart casts?
zhombie opened this issue · 4 comments
zhombie commented
what about smart casts?
zhombie commented
As you can see here below, IDE will automatically detect whether the variable is nullable or not within the certain block
https://kotlinlang.org/docs/reference/typecasts.html#smart-casts
zhombie commented
It would be great if your library provides smart casts & there would be no need of using !! symbol, in order to use variable in already checked if statement block, for example,
val nullableVariable: String? = null
whatIfNot(nullableVariable) {
nullableVariable -> smart casted
}
skydoves commented
It is already using the smart casts and you can get a smart casted field via the receiver.
nullableString.whatIfNotNull {
val length = it.length
log("$it is not null.")
}
Every whatIf
keywords can be used like scope functions. e.g.let
, apply
, run
.
You can check more example codes here.