oday
10/31/2018, 3:04 PMval newFragment: Fragment? = if (fragment is CarSearchFragment? && fragment == null) {
CarSearchFragment()
} else if (fragment is CarValuationFragment? && fragment == null) {
CarValuationFragment()
} else if (fragment is HotDealsFragment? && fragment == null) {
HotDealsFragment()
} else if (fragment is FavoritesFragment? && fragment == null) {
FavoritesFragment()
} else {
fragment
}
louiscad
10/31/2018, 3:06 PMis
check on null
. Doesn't the IDE warns you about this?oday
10/31/2018, 3:06 PMis
is for type not valuewhen
I guessdalexander
10/31/2018, 3:14 PMval newFragment = if(fragment != null) {
when() {
...
}
} else {
fragment
}
or
val newFragment = fragment ?: when(fragment) {
...
}
Maybe one of these?oday
10/31/2018, 3:17 PMdalexander
10/31/2018, 3:17 PMval newFragment = fragment.initIfNull()
or something, and that lets you avoid doing all the type checking which generally is good to avoid for OO-design.oday
10/31/2018, 3:17 PMShawn
10/31/2018, 4:25 PMnull
and this much type checking is no bueno in general unless you’re working with enum or sealed classes with minimal stateAndreas Sinz
11/01/2018, 10:19 AMval newFragment = fragment?.let { when(it) {
is CarSearchFragment? -> CarSearchFragment()
is CarValuationFragment? -> CarValuationFragment()
...
}
Shawn
11/01/2018, 11:15 AMnull
will always hit the first branch of the when regardless of what type it might’ve had at the call siteAndreas Sinz
11/01/2018, 11:30 AModay
11/01/2018, 12:21 PM