o
ahhh
m
Yes, ? is part of the type.
o
does when(fragment){ is MyType, null -> { } } mean if fragment is of myType and is null?
m
would have to be
is MyType?
although I think you could do
Copy code
when(fragment) {
  is MyType -> {}
  is MyType? -> {}
}
instead. Although neither ‘feels’ right. Maybe
is MyType? -> fragment?.let { //not null} ?: {null}
I think ‘,’ is correct for AND in when, although I think it changed to &&. Your condition would never be true, though as if it’s a
MyType
it can’t possibly be null.
o
oh I meant
MyType?
again sorry
issue is that im trying to automate the function of assigning the correct instantiation class to an object, all the classes it can have derive from 1 class, so I have to know which I want exactly
so if it’s from this type and it still isnt instantiated, give it ThisClassB(), otherwise just leave it as is
and use it after the when
let me show code one second
Copy code
private 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()
        }
    }
m
Hmm. I’ll trust that you do need the code, and I assume the ’CarSearchFragment?, null` is compiling, correct? If so, I can post a fragment that’s a bit cleaner. Also, if fragment is already the correct type, why is a new one constructed into newFragment and then never used (as fragment != null in order to get into those branches.
o
well, because I got that val cannot be reassigned when I tried to assign it 😄
it is used in the end if you notice though
in its pristine state where it was passed as is and I didnt reassign it to anything, I use it to show it, otherwise I show the new one which was passed first as null
m
Are ‘?’ missing for CarValuationFragment branches? Otherwise if it’s a CarValuationFragment (non-null), newFragment gets assigned a new one. BUT fragment is != null, so it will use the original fragment. Therefore the newFragment assigned variable isn’t used. And I suspect you’ve forgotten that things in Kotlin are expressions.
Copy code
val newFragment = when (fragment) {
        is CarSearchFragment?, null -> {
            CarSearchFragment()
        }
        is CarValuationFragment -> {
            CarValuationFragment()
        }
        is HotDealsFragment -> {
            HotDealsFragment()
        }
        is FavoritesFragment -> {
            FavoritesFragment()
        }
        else -> fragment
    }
or something like that. Wasn’t 100% sure what to put in the
else
o
they are not
?
yet cause i hadnt gotten to them yet
so I didnt edit those parts yet, still working on the first branch
m
Ok, wasn’t sure. That makes more sense.
o
ok so yea yours makes more sense
m
Given that, I think the ‘else’ would return
null
rather than what I have right now
o
ah there must be an else yea
m
If you don’t assign a
when
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.
Hmmm, and right now, if a new subtype of Fragment is added, you won’t have any notice, and may try and use a null ‘newFragment’ in your assignment. If possible, I’d consider a
sealed class
here so the
when
will ensure all types are handled correctly.
o
well I control what fragments are added and how they’re passed into this function
my knowledge of those classes is the limiting factor of how many classes this function can accept 😄
m
sealed class
gets the compiler to help you, though. But obviously it’s up to you.
o
I see here that those duplicate null conditions are coming up as just that,
duplicate labels in when
I think an if is better here..
that
,
is not behaving as I expect
m
I think the comma is only good if the items being compared are the same type. Not sure as I haven't used it yet.