I have this sealed class PictureEvent
:
sealed class PictureEvent {
data class PictureCreated(val pictureId: String, val url: String) : PictureEvent()
//more classes extending PictureEvent
}
Now, from a list of PictureEvent
s, I want to get the first PictureCreated
:
fun doSomething(events: List<PictureEvent>) {
val creationEvent = events.first { isCreationEvent(it) } as PictureEvent.PictureCreated
//do stuff with the creationEvent
}
private fun isCreationEvent(event: PictureEvent) : Boolean {
return event is PictureEvent.PictureCreated
}
It works fine. As you see, I'm casting the event to PictureCreated
(using as
keyword), since first
method, returns a PictureEvent
. I wonder if it's possible to avoid this casting by using Kotlin contracts.
I have tried this:
private fun isCreationEvent(event: PictureEvent) : Boolean {
contract {
returns(true) implies (event is PictureEvent.PictureCreated)
}
return event is PictureEvent.PictureCreated
}
But it doesn't work; first
method keep returning a PictureEvent
, instead of PictureCreated
. Is it possible to do this currently?
isCreationEvent()
? – Animesh Sahuevents.first { isCreationEvent(it) }
– Héctor