https://kotlinlang.org logo
Title
s

speed_star

11/17/2020, 8:33 AM
Hi. I'd like to check whether
null
property is exists in data class. Should I check for each property like as below? Or there is other good way?
data class Book(val name: String?, val author: String?, val price: Int?)

fun isExistNull(): Boolean {
    if (name == null || author == null || price == null) {
        return true
    }
    return false
}
j

jbnizet

11/17/2020, 12:41 PM
return (name == null || author == null || price == null)
is sufficient. But other than that, yes, it’s the straightforward way to do it.
👍 1
n

Nick Johnson

11/17/2020, 10:38 PM
name ?: author ?: price ?: true == true
should also work
👍 1