Is it currently not supported smart-cast inside `w...
# compose
m
Is it currently not supported smart-cast inside
when
for the
sealed class
with
LiveData.observeAsState
like this code in 🧵?
Copy code
// we implement this on VM, e.g: class CurrentOccupationViewModel : CurrentOccupationState
interface CurrentOccupationState {
    val hasNoWorkExperience: LiveData<NoWorkExperienceState> // we're still not using <Flow> in this phase of compose migration
    fun hasNoWorkExperience(hasNoWorkExperience: NoWorkExperienceState)
}
Copy code
// CurrentOccupationScreen.kt

sealed class NoWorkExperienceState {
    data class NullExperience(val message: String) :
        NoWorkExperienceState()
    object WithExperience : NoWorkExperienceState()
}

@Composable
fun CurrentOccupationScreen(state: CurrentOccupationScreen) {
    val hasNoWorkExperience by state.hasNoWorkExperience.observeAsState()
    when(hasNoWorkExperience){
        is NoWorkExperienceState.NullExperience -> {
            val nullExperience = hasNoWorkExperience as NoWorkExperienceState.NullExperience
            println(nullExperience.message)
            // Smart cast to 'NoWorkExperienceState.NullExperience' is impossible, because 'hasNoWorkExperience' is a property that has open or custom getter       
            println(hasNoWorkExperience?.message) 
            // Showing toast for now
        }
        NoWorkExperienceState.WithExperience -> {}
    }
}
Any other suggestion for the above case? or we should use manual casting
as
, without smart cast (because it’s intended/impossible as of now?) The above code showing below error:
Copy code
Smart cast to 'NoWorkExperienceState.NullExperience' is impossible, because 'hasNoWorkExperience' is a property that has open or custom getter
c
Copy code
when(val temp = hasNoWorkExperience){
   is NoWorkExperienceState.NullExperience -> {
      println(temp.message)
   }
}
👀 1
🆙 1
m
Splendid! Thank you so much @carbaj0 it works seamlessly (left image). The only concern was the (right image), it suggest us to
inline
the code, which back to the above issues.
l
I also experienced the same thing; see https://youtrack.jetbrains.com/issue/KTIJ-18925.
🙏 2
2
m
Nice, this was resolved in 1.5.10 K