henrik
11/22/2018, 8:56 AMfun toMap(): Map<String, Double> = mapOf("key" to nullableDouble1, "key2" to nullableDouble2).filterValues { it != null }
complains that "expected Map<String, Double>
but found `Map<String, Double?>`". Is there an easy way around this other than appending as Map<String, Double>
?spand
11/22/2018, 9:14 AM.entries.mapNotNull { (k,v) -> v?.let {k to v} }.toMap()
.mapValuesNotNull()
does not existhenrik
11/22/2018, 9:17 AM.mapValues { it.value ?: 0.0 }
at the endkarelpeeters
11/22/2018, 9:31 AM.mapValues { it!! }
is safer.henrik
11/22/2018, 9:32 AM!!
in kotlin codekarelpeeters
11/22/2018, 9:33 AM?: 0
just hides the issue and you're back to not actually having null safety.null
with 0.0
.henrik
11/22/2018, 9:34 AMmapValues
is just a dummykarelpeeters
11/22/2018, 9:34 AM.mapValues { it!! }
is a safer dummy that will throw if someone ever changes the map call.henrik
11/22/2018, 9:35 AMmapValues { it.value!! }
karelpeeters
11/22/2018, 9:39 AM{{}}
?henrik
11/22/2018, 9:40 AMfun toMap(): Map<String, Double> =
mapOf("key" to 1.1, "key2" to null)
.mapNotNull { it.value?.let { value -> it.key to value } }
.toMap()