I have the following class ```sealed class ApiResult<out T:Any> { data class Success<ou...
g
I have the following class
Copy code
sealed class ApiResult<out T:Any> {
    data class Success<out T:Any>(val value : T) : ApiResult<T>()
    data class Error(val message : String, val cause: Exception? = null) : ApiResult<Nothing>()
    object Loading : ApiResult<Nothing>()
}
But when i try to use it like this:
Copy code
val result by viewModel.allPositions.collectAsStateWithLifecycle(initialValue = ApiResult.Loading)
when (result) {
    is ApiResult.Error -> {
        item {
            Text("Error: ${result.message}")
        }
    }
I get: Smart cast to 'ApiResult.Error' is impossible, because 'result' is a property that has open or custom getter
m
You cannot not use
by
and smart casting.
by
makes the property a computed property and the compiler doesn't know that it will not change (I'm not sure that it won't) between the type check and the use. Instead you will have to do
val result = viewModel.allPositions.collectAsStateWithLifecycle(initialValue = ApiResult.Loading).value
❤️ 1
g
Thanks.