skydoves/WhatIf

what about smart casts?

zhombie opened this issue · 4 comments

what about smart casts?

Hi, @zhombie
Could you explain more details about the smart casts?

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

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
}

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.