https://kotlinlang.org logo
#getting-started
Title
# getting-started
i

IbrahimAbousalem

10/10/2021, 7:05 PM
Hi guys I was trying to filter upon a sealed class-specific state but the filter function didn't work. Does anyone know how to do filter against sealed classes state? for example:
Copy code
sealed class States{
object Loading:States()
data Success(val data: String):States()
}

results.filter{
 it.state is States.Success 
}.forEach{ successState ->
println(successState.data) //Error since this still State not Success State
}
j

Joffrey

10/10/2021, 7:07 PM
You can use
filterIsInstance<States.Success>()
instead of a classic
filter
to have the correct type down the line
👍 2
i

IbrahimAbousalem

10/10/2021, 7:18 PM
Oh, I didn't notice this function. Thanks for your help.
90 Views