Hello! I have a number of classes that might or might not be resolved to some external data source. So that just means wrapping in a container. A combination of optional / decorator pattern.
sealed class Resolvable<T>
class Resolved<T>(val value: T, val pk: Long) : Resolvable<T>()
class Unresolved<T>(val value: T) : Resolvable<T>() {
fun resolved(pk: Long) = Resolved(value, pk)
}
Now I want to take a
Collection<Resolvable<ItemIdentifier>>
and filter only unresolved ones, into a
Collection<Unresolved<ItemIdentifier>>
.
This works:
val x : Collection<Unresolved<ItemIdentifier>> = itemIdentifiers.mapNotNull { when(it) {is Unresolved -> it else -> null }}
But I wonder if there's a more idiomatic way to do it? (type annotation on
x
just for clarity).