https://kotlinlang.org logo
Title
w

waqas

02/23/2018, 11:43 AM
What'e the idiomatic way checking null on multiple variables and passing those to a function which takes not nulls? I went through this -> https://discuss.kotlinlang.org/t/kotlin-null-check-for-multiple-nullable-vars/1946/8 I liked this approach but is there something better?
inline fun <A, B, R> ifNotNull(a: A?, b: B?, code: (A, B) -> R) {
    if (a != null && b != null) {
        code(a, b)
    }
}
...
    fun test() {
        ifNotNull(name, age) { name, age ->
            doSth(name, age)
        }
    }
b

bdawg.io

08/08/2018, 6:38 PM
I’m really not sure why my Slack is still back in February lol. But you could also do
fun test() {
    ignoreNpe {
        doSth(requireNotNull(name), requireNotNull(age))
    }
}