Thread
#compose
    m

    miqbaldc

    1 year ago
    Is it currently not supported smart-cast inside
    when
    for the
    sealed class
    with
    LiveData.observeAsState
    like this code in 🧵?
    // 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)
    }
    // 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:
    Smart cast to 'NoWorkExperienceState.NullExperience' is impossible, because 'hasNoWorkExperience' is a property that has open or custom getter
    carbaj0

    carbaj0

    1 year ago
    when(val temp = hasNoWorkExperience){
       is NoWorkExperienceState.NullExperience -> {
          println(temp.message)
       }
    }
    m

    miqbaldc

    1 year ago
    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

    lhwdev

    1 year ago
    m

    miqbaldc

    1 year ago
    Nice, this was resolved in 1.5.10 :kotlin-intensifies: