https://kotlinlang.org logo
#android
Title
# android
a

Ali

04/23/2020, 9:49 AM
What's the best way to handle this?
response.some1?.let { some1 -> response.some2?.let { some2 -> some1 + some2 } }
h

Hakob Vardanyan

04/23/2020, 9:55 AM
Copy code
fun <T1 : Any, T2 : Any, R : Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2) -> R?): R? {
    return if (p1 != null && p2 != null) block(p1, p2) else null
}
And use like this
Copy code
safeLet(o1, o2) { obj1, obj2 ->
    // do something
}
👍 2
l

LopsPower

04/23/2020, 9:56 AM
Copy code
response.takeIf { it.some1 != null && it.some2 != null }
    .let { it.some1 + it.some2 }