bbaldino
07/09/2019, 8:38 PMwithDefault for a map doesn't actually insert that default into the map when a key which isn't present is found, it just returns an instance of that default?nfrankel
07/09/2019, 8:45 PMpublic fun <K, V> Map<K, V>.withDefault(defaultValue: (key: K) -> V): Map<K, V> =
when (this) {
is MapWithDefault -> this.map.withDefault(defaultValue)
else -> MapWithDefaultImpl(this, defaultValue)
}
so your assumption is correctbbaldino
07/09/2019, 8:47 PMkarelpeeters
07/09/2019, 8:47 PMgetValue source @nfrankelkarelpeeters
07/09/2019, 8:48 PMval map = mutableMapOf<String, Int>().withDefault { 5 }
println(map.getValue("test"))
println(map.keys)karelpeeters
07/09/2019, 8:48 PMbbaldino
07/09/2019, 8:48 PMkarelpeeters
07/09/2019, 8:49 PMbbaldino
07/09/2019, 8:49 PMgetOrDefaultkarelpeeters
07/09/2019, 8:49 PMgetOrPut although you then have to repeat the default value.bbaldino
07/09/2019, 8:50 PMgetOrPut. i think i can live with that, though the other style would've been nice. i run into this somewhat often when having a map of some key to a listdiesieben07
07/10/2019, 7:35 AMget violates the Map interface contract and thus is a bad idea.bbaldino
07/10/2019, 8:38 PMDefaultDict in python (that was actually my initial interpretation of what withDefault did: return me a wrapped map type which had that behavior)