I have the following code:
class Countries {
var list: MutableList<String>? = null
}
val countries = Countries()
if (countries.list!!.isNotEmpty()) {
}
At runtime this will raise an exception because list is null. I can do this instead:
if ((countries.list != null) && countries.list!!.isNotEmpty()) {
}
If I had a boolean member called areInitialized that was nullable, I could create a infix function like this:
infix fun Any?.ifTrue(block: () -> Unit) {
if ((this != null) && this == true) block()
}
and then use it like this:
countries.areInitialized ifTrue {
}
But I can't seem to create something similar for a mutable list.
But I hate having to repeat this test for null on an member field in other parts of code. Is there a simpler way in Kotlin to do this?