# Map.withDefault not defaulting?

**URL:** <https://discuss.kotlinlang.org/t/map-withdefault-not-defaulting/7691>\
**Category:** Language Design\
**Created:** [May 4, 2018, 9:50pm UTC](https://discuss.kotlinlang.org/t/map-withdefault-not-defaulting/7691 "2018-05-04T21:50:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [May 4, 2018, 9:50pm UTC](https://discuss.kotlinlang.org/t/map-withdefault-not-defaulting/7691/1 "2018-05-04T21:50:42Z")

</div>

Maybe I’m missing something silly, but this prints “null” instead of “0”

```
val wordCount = mutableMapOf<String, Int>().withDefault { 0 }
println(wordCount["hi"])

```

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [May 4, 2018, 10:00pm UTC](https://discuss.kotlinlang.org/t/map-withdefault-not-defaulting/7691/2 "2018-05-04T22:00:24Z")

</div>

> This implicit default value is used when the original map doesn’t contain a value for the key specified and a value is obtained with **`Map.getValue`** function, for example when properties are delegated to the map.

So you need to use `wordCount.getValue("hi")`

---

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [May 4, 2018, 10:38pm UTC](https://discuss.kotlinlang.org/t/map-withdefault-not-defaulting/7691/3 "2018-05-04T22:38:17Z")

</div>

errr… I’m guessing that the general expectation is Maps use get(). The IntelliJ IDE tells me “don’t use get(), use ” Which I was happy to do.

Won’t everyone fall into the same trap and expect “withDefault” to work when `get`-ting the usual way? I certainly did.

If I wanted to edit all my getter lines, I’d use `getOrDefault`

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [May 4, 2018, 11:16pm UTC](https://discuss.kotlinlang.org/t/map-withdefault-not-defaulting/7691/4 "2018-05-04T23:16:27Z")

</div>

Unfortunately altering the behavior of `get` when there’s no value for a given key will violate the `Map` interface contract.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [May 4, 2018, 11:19pm UTC](https://discuss.kotlinlang.org/t/map-withdefault-not-defaulting/7691/5 "2018-05-04T23:19:54Z")

</div>

This issue [https://youtrack.jetbrains.com/issue/KT-11851](https://youtrack.jetbrains.com/issue/KT-11851) has some prior discussion on this topic.

---

<div class="post-metadata">

**Author:** ![swapdatname](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@swapdatname](https://discuss.kotlinlang.org/u/swapdatname)\
**Post date:** [November 8, 2023, 11:11pm UTC](https://discuss.kotlinlang.org/t/map-withdefault-not-defaulting/7691/6 "2023-11-08T23:11:20Z")

</div>

It seems that Kotlin already has an interface called `MapWithDefault`. I’m sure many people would be happy if `withDefault` would return a collection of that type.
