https://kotlinlang.org logo
#announcements
Title
# announcements
g

ghosalmartin

03/21/2017, 4:09 PM
If I have a data class thats mostly nullables, is there anyway to quickly cast all of them to non null? or is it just a matter of having to do the null checks manually?
m

miha-x64

03/21/2017, 4:13 PM
Copy code
data class Nullables(val first: String?, val second: Any?) {
    fun nonNull() = NonNull(first!!, second!!)
}
data class NonNull(val first: String, cal second: Any)
g

ghosalmartin

03/21/2017, 4:13 PM
Needs to be a safe way to do it I guess, hmm
m

miha-x64

03/21/2017, 4:14 PM
Copy code
fun nonNullOrNull(): NonNull? {
    val fst = first ?: return null
    val snd = second ?: return null
    return NonNull(fst, snd)
}
2 Views