kotlin-contracts

Using Kotlin contracts to cast type inside Iterable function predicate

ⅰ亾dé卋堺 提交于 2020-05-24 03:29:24
问题 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

Using Kotlin contracts to cast type inside Iterable function predicate

为君一笑 提交于 2020-05-24 03:29:02
问题 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