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

前端 未结 7 806
鱼传尺愫
鱼传尺愫 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:21

    There's a nice, little lib that allows for writing let-like code with multiple variables. It's open-source and you can find it on GitHub, it's called Unwrap

    Example based on readme:

    unwrap(_a, _b, _c) { a, b, c ->
        println("$a, $b$c") // all variables are not-null
    }
    

    All unwrap(...) methods are marked inline so there should be no overhead with using them.

    By the way, this lib also allows to handle situation when there are some null variables (the nah() method).

提交回复
热议问题