filter doesn't change the type, it only removes el...
# announcements
j
filter doesn't change the type, it only removes elements
j
Removing all the null values would however effectively change the type from nullable String to string? So a smart cast seems sensible to me here?
j
Smart cast works on
it
inside the lambda
filter is a function and it's return type is defined as the same type of its receiver
a
afaik not much you can do but to cast it 😞 but at least you can isolate that in a single place:
Copy code
@Suppress("UNCHECKED_CAST")
fun <K, V> Map<K, V?>.filterNonNullValues() = filterValues { it != null } as Map<K, V>
j
That’s what I ended up doing @araqnid, thanks 🙂