Multiple variable let in Kotlin

后端 未结 12 1153
失恋的感觉
失恋的感觉 2020-11-30 18:48

Is there any way to chain multiple lets for multiple nullable variables in kotlin?

fun example(first: String?, second: String?) {
    first?.let {
        se         


        
12条回答
  •  既然无缘
    2020-11-30 19:25

    I solved this by creating some functions that more or less replicates the behavior of with, but takes multiple parameters and only invokes the function of all the parameters is non-null.

    fun  withNoNulls(p1: A?, p2: B?, function: (p1: A, p2: B) -> R): R? = p1?.let { p2?.let { function.invoke(p1, p2) } }
    fun  withNoNulls(p1: A?, p2: B?, p3: C?, function: (p1: A, p2: B, p3: C) -> R): R? = p1?.let { p2?.let { p3?.let { function.invoke(p1, p2, p3) } } }
    fun  withNoNulls(p1: A?, p2: B?, p3: C?, p4: D?, function: (p1: A, p2: B, p3: C, p4: D) -> R): R? = p1?.let { p2?.let { p3?.let { p4?.let { function.invoke(p1, p2, p3, p4) } } } }
    fun  withNoNulls(p1: A?, p2: B?, p3: C?, p4: D?, p5: E?, function: (p1: A, p2: B, p3: C, p4: D, p5: E) -> R): R? = p1?.let { p2?.let { p3?.let { p4?.let { p5?.let { function.invoke(p1, p2, p3, p4, p5) } } } } }
    

    Then I use it like this:

    withNoNulls("hello", "world", Throwable("error")) { p1, p2, p3 ->
        p3.printStackTrace()
        p1.plus(" ").plus(p2)
    }?.let {
        Log.d("TAG", it)
    } ?: throw Exception("One or more parameters was null")
    

    The obvious issue with this is that I have to define a function for each case (number of variables) I need, but at least I think the code looks clean when using them.

提交回复
热议问题