# Decrease a value of any countable type (map)

**URL:** <https://discuss.kotlinlang.org/t/decrease-a-value-of-any-countable-type-map/25811>\
**Category:** Support\
**Created:** [October 16, 2022, 9:03am UTC](https://discuss.kotlinlang.org/t/decrease-a-value-of-any-countable-type-map/25811 "2022-10-16T09:03:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [October 16, 2022, 9:03am UTC](https://discuss.kotlinlang.org/t/decrease-a-value-of-any-countable-type-map/25811/1 "2022-10-16T09:03:37Z")

</div>

I’m just trying to unify the map extension

```auto
fun <T> MutableMap<T, Int>.dec(key: T, value: Int = 1) = merge(key, value, Int::minus)

```

and stuck. How to make it with any countable type?

```auto
fun <K, V> MutableMap<K, V>.dec(key: K, value: Comparable<V>) = merge(key, value, Comparable::minus)

```

Actually, it’s possible to do a type converter like

```auto
when (value) {
  is Int -> Int
  is Float -> Float
  else Int
}

```

And I remember, there was something alike, but how to make it short?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [October 16, 2022, 9:26am UTC](https://discuss.kotlinlang.org/t/decrease-a-value-of-any-countable-type-map/25811/2 "2022-10-16T09:26:32Z")

</div>

I don’t think this is possible without using reflection. And even with reflection it won’t work with extension functions, only with member functions.

Kotlin doesn’t have a concept of a “countable type” as you call it and sadly, operators are not represented as interfaces as well. You can create such interface for your own classes and then you can make a generic `dec()` function, but it won’t work for existing types.

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [October 16, 2022, 9:42am UTC](https://discuss.kotlinlang.org/t/decrease-a-value-of-any-countable-type-map/25811/3 "2022-10-16T09:42:51Z")

</div>

Maybe it’s possible to define V in MutableMap\<K, V\> and then make personal calculations?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [October 16, 2022, 11:35am UTC](https://discuss.kotlinlang.org/t/decrease-a-value-of-any-countable-type-map/25811/4 "2022-10-16T11:35:06Z")

</div>

Similar solutions are implemented in KMath. Inside algebra, it is possible to perform sum operation on a type the algebra covers. It is also possible to infer algebra in runtime, but it is not effective.
