What's the best way to handle this? `response.som...
# android
a
What's the best way to handle this?
response.some1?.let { some1 -> response.some2?.let { some2 -> some1 + some2 } }
h
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
Copy code
response.takeIf { it.some1 != null && it.some2 != null }
    .let { it.some1 + it.some2 }