Colton Idle
10/02/2019, 3:02 AMif (viewModel.value?.items()?.subItems != null) {
//Do my thing
}
I don't think this is bad because it's straight forward. As long as that statement is not null I want to execute some code. Is there a more idiomatic way of doing this?Amirul Zin
10/02/2019, 3:08 AMviewModel.value?.items()?.subItems?.let { //or run, apply, etc..
//do your thing
}
I personally prefer with
. Feels quite declarative:
with(viewModel.value?.items()?.subItems?) {
map(propertyA)
println(size)
//example
}
Alex Crafford
10/02/2019, 12:36 PMviewmodel?.value?.items()?.subItems?.let { doThing(it) } ?: handleNullSubItems()
Kirill Zhukov
10/02/2019, 3:32 PMAlex Crafford
10/02/2019, 4:22 PMKirill Zhukov
10/02/2019, 6:07 PMprivate val ViewModel?.subItems: List<SubItemType> get() = value?.items()?.subItems.orEmpty()
and then use a normal if statement (which is more readable imo but it really depends on the situation), also assuming that items being null or empty list is semantically same thing to youColton Idle
10/02/2019, 6:46 PMkarelpeeters
10/02/2019, 10:06 PM