oday
10/31/2018, 1:52 PMMike
10/31/2018, 2:13 PModay
10/31/2018, 2:23 PMMike
10/31/2018, 2:25 PMis MyType?
although I think you could do
when(fragment) {
is MyType -> {}
is MyType? -> {}
}
instead. Although neither ‘feels’ right.
Maybe is MyType? -> fragment?.let { //not null} ?: {null}
Mike
10/31/2018, 2:28 PMMyType
it can’t possibly be null.oday
10/31/2018, 2:28 PMMyType?
again sorryoday
10/31/2018, 2:29 PModay
10/31/2018, 2:29 PModay
10/31/2018, 2:29 PModay
10/31/2018, 2:30 PModay
10/31/2018, 2:31 PMprivate fun openBottomBarFragment(fragment: Fragment?) {
var newFragment: Fragment? = null
when (fragment) {
is CarSearchFragment?, null -> {
newFragment = CarSearchFragment()
}
is CarValuationFragment -> {
newFragment = CarValuationFragment()
}
is HotDealsFragment -> {
newFragment = HotDealsFragment()
}
is FavoritesFragment -> {
newFragment = FavoritesFragment()
}
}
if (fragment == null) {
supportFragmentManager.beginTransaction().add(
R.id.fragment_container, newFragment!!
).commit()
} else {
supportFragmentManager.beginTransaction()
.hide(activeFragment).show(fragment)
.commit()
}
}
Mike
10/31/2018, 2:35 PModay
10/31/2018, 2:38 PModay
10/31/2018, 2:41 PModay
10/31/2018, 2:41 PMMike
10/31/2018, 2:44 PMval newFragment = when (fragment) {
is CarSearchFragment?, null -> {
CarSearchFragment()
}
is CarValuationFragment -> {
CarValuationFragment()
}
is HotDealsFragment -> {
HotDealsFragment()
}
is FavoritesFragment -> {
FavoritesFragment()
}
else -> fragment
}
Mike
10/31/2018, 2:44 PMelse
oday
10/31/2018, 2:45 PM?
yet cause i hadnt gotten to them yetoday
10/31/2018, 2:45 PMMike
10/31/2018, 2:46 PModay
10/31/2018, 2:46 PMMike
10/31/2018, 2:46 PMnull
rather than what I have right nowoday
10/31/2018, 2:46 PMMike
10/31/2018, 2:48 PMwhen
to a variable, it doesn’t do the branch completion check. As soon as you assign it to something, it then enforces the branch check.
I think there’s a KEEP requesting that when always be complete, but not yet.Mike
10/31/2018, 2:49 PMsealed class
here so the when
will ensure all types are handled correctly.oday
10/31/2018, 2:50 PModay
10/31/2018, 2:50 PMMike
10/31/2018, 2:51 PMsealed class
gets the compiler to help you, though. But obviously it’s up to you.oday
10/31/2018, 2:51 PMduplicate labels in when
oday
10/31/2018, 2:53 PModay
10/31/2018, 2:53 PM,
is not behaving as I expectMike
10/31/2018, 2:56 PM