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()spand
11/22/2018, 9:14 AM.mapValuesNotNull() does not existspand
11/22/2018, 9:16 AMspand
11/22/2018, 9:16 AMhenrik
11/22/2018, 9:17 AM.mapValues { it.value ?: 0.0 } at the endhenrik
11/22/2018, 9:18 AMkarelpeeters
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.karelpeeters
11/22/2018, 9:33 AMnull with 0.0.henrik
11/22/2018, 9:34 AMhenrik
11/22/2018, 9:34 AMmapValues is just a dummyhenrik
11/22/2018, 9:34 AMkarelpeeters
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 AMhenrik
11/22/2018, 9:37 AMmapValues { it.value!! }karelpeeters
11/22/2018, 9:39 AMkarelpeeters
11/22/2018, 9:39 AM{{}}?henrik
11/22/2018, 9:40 AMhenrik
02/06/2019, 1:09 PMfun toMap(): Map<String, Double> =
mapOf("key" to 1.1, "key2" to null)
.mapNotNull { it.value?.let { value -> it.key to value } }
.toMap()