I'm trying to think of a function that would allow a Map<String, Any?>
object to be treated as Map<String,Any>
through type inference through applying a single function.
I am pretty new to the transformation functions in Kotlin and have tried the various filter
and filterValues
filterNot
on the map like so:
val input = mapOf(Pair("first",null))
val filtered: Map<String,Any> = input.filter { it.value!=null }
it also fails to compile with any of these
input.filterValues { it!=null }
input.filterNot { it.value==null }
input.filterNot { it.value is Nothing }
The closest I can seem to get is applying multiple steps or having an Unchecked cast warning. I would have thought that filtering the values to be !=null
would suffice. My only other thought is that it's due to the generics?
mapValues
be simplified or are we stuck with taking in theMap.Entry
? It seems weird that it wouldn't just provide the value of type V and expect a return of type R to me but I suppose that would incur a performance cost that iterating over an entry set doesn't. Maybe the function name is too ambiguous? – Idolize