https://kotlinlang.org logo
#codereview
Title
# codereview
m

Mark

12/30/2020, 12:11 PM
I’m surprised there is not something for this in the standard lib. Is this okay or maybe remove inline/reified?
Copy code
inline fun <reified K, V> Map<out K?, V>.filterKeyNotNull(): Map<K, V> =
    mapNotNull { (key, value) -> if (key != null) key to value else null }.toMap()
m

Milan Hruban

12/30/2020, 12:34 PM
why not use
map.filterKeys { it != null }
m

Mark

12/30/2020, 1:12 PM
It returns type `Map<K?, V>`:
Copy code
fun <K, V> Map<out K, V>.filterKeys(predicate: (K) -> Boolean): Map<K, V>
It could be cast like this:
Copy code
fun <K, V> Map<out K?, V>.filterKeyNotNull(): Map<K, V> =
    filterKeys { it != null } as Map<K, V>
i

Ivan Pavlov

12/30/2020, 3:14 PM
I have 3 years experience and I've never used maps with nullable keys. If it is not in standard library probably it is really rare use case and it's just not worth to add such function