I have a `List<Pair<A,B?>>` Is there a...
# announcements
p
I have a
List<Pair<A,B?>>
Is there an easy way to filter out all where
second==null
and make the type be
List<Pair<A,B>>
?
k
Not, you'll have to cast it.
b
You can try mapNotNull - convert
Pair<A,B?>
To
Pair<A,B>?
k
How to express this is still an open question for contracts: https://github.com/Kotlin/KEEP/blob/master/proposals/kotlin-contracts.md#open-questions
p
Thanks
b
Something like
list.mapNotNull{if (it.second != null) it.first to it.second else null}
p
nope, that didn’t work. The inferred type is still
List<Pair<A,B?>>
k
You need an intermediate variable, something like
Copy code
list.mapNotNull {
    val second = it.second
    if (second == null) null else it.first to second
}
b
list.mapNotNull { (first, second) -> second?.let { first to second } }
1
p
ooo, nice
thank you