dasz
03/16/2018, 5:28 PMopen class Human()
class Men:Human()
class Women:Human()
val humanNation = asList(Women(), Man())
val womenForMe = humanNation.firstOrNull{ it is Women} as Women?
any shorter form?
thank you @agrosner for the help the solution is
inline fun <reified K:T, T> List<T>.firstOrNull(): K? = firstOrNull { it is K} as K?
usage
val womenForMe : Women = humanNation.firstOrNull()
zpearce
03/16/2018, 5:35 PMWomen?
agrosner
03/16/2018, 5:35 PMfred.deschenes
03/16/2018, 5:36 PMfilterIsInstance
but that's not shorter and will go through the whole listShawn
03/16/2018, 5:36 PMHuman
type that was being implementedkristofdho
03/16/2018, 5:36 PM.first()
agrosner
03/16/2018, 5:36 PMinline fun <reified T> List<T>.singleOrNull(): T? = singleOrNull { it is T} as T?
dasz
03/16/2018, 5:58 PMinline fun <reified K:T, T> List<T>.firstOrNull(): K? = firstOrNull { it is K} as K?
agrosner
03/16/2018, 6:57 PM