Best way to handle such scenario where “smart cast is imposible”

前端 未结 7 799
鱼传尺愫
鱼传尺愫 2020-12-20 17:33

I wonder what is the best way to handle such scenario

class Person(var name:String? = null, var age:Int? = null){
    fun test(){
        if(name != null &am         


        
7条回答
  •  [愿得一人]
    2020-12-20 18:15

    For the cast to be possible you have to make a local copy of the value somehow. In Kotlin this is best done explicitly:

    val name = name
    val age = age
    if(name != null && age != null){
        doSth(name, age)
    }
    

    The let function hides this behind an abstraction layer, which is not the best IMHO.

提交回复
热议问题