# Access value by type with any kind of data structure

**URL:** <https://discuss.kotlinlang.org/t/access-value-by-type-with-any-kind-of-data-structure/30348>\
**Category:** Uncategorized\
**Created:** [May 10, 2025, 9:38am UTC](https://discuss.kotlinlang.org/t/access-value-by-type-with-any-kind-of-data-structure/30348 "2025-05-10T09:38:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalayda](https://avatars.discourse-cdn.com/v4/letter/k/f05b48/32.png) [@kalayda](https://discuss.kotlinlang.org/u/kalayda)\
**Post date:** [May 10, 2025, 9:38am UTC](https://discuss.kotlinlang.org/t/access-value-by-type-with-any-kind-of-data-structure/30348/1 "2025-05-10T09:38:25Z")

</div>

Suppose I need to keep three numbers of `Int`, `Long` and `Float` in a bunch, and access these numbers by their type.  
First idea is to use `HashMap`, where key is type of number, and value is number itself.

```auto
val bunch = mapOf<KClass<out Number>, Number>(
    Int::class to 0,
    Long::class to 0L,
    Float::class to 0f,
)

```

But in this case Kotlin does not inference needed value type, and I need to use explicit cast.

```auto
val i: Int = bunch[Int::class]!! // Error: Initializer type mismatch: expected 'Int', actual 'Number'.
val f: Float = bunch[Int::class]!! as Float

```

Also this does not guarantee that value has the type, spcecified by the key. So I can do

```auto
val bunch = mapOf<KClass<out Number>, Number>(
    Int::class to 0L, // Long value for Int type
)

val l: Float = bunch[Int::class]!! as Float // Get Int as Float

```

Can you suggest more appropriate solution for the problem (access value by type with any kind of data structure)?

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [May 10, 2025, 9:43am UTC](https://discuss.kotlinlang.org/t/access-value-by-type-with-any-kind-of-data-structure/30348/2 "2025-05-10T09:43:59Z")

</div>

I made this typed Map:

```kotlin
private class MutableTypedMap private constructor(private val map: MutableMap<Key<*>, Any?>) {
  constructor() : this(mutableMapOf())

  interface Key<T>

  @Suppress("UNCHECKED_CAST")
  operator fun <T> get(key: Key<T>): T = map.getValue(key) as T

  operator fun <T> set(key: Key<T>, value: T) {
    map[key] = value
  }
}

```

You can replace `Key<T>` with `KClass`, and everything should work just fine!  
Edit: Here you go!

```run-kotlin
import kotlin.reflect.*
val bunch = MutableTypedMap().apply {
    this[Int::class] = 0
    this[Long::class] = 0L
    this[Float::class] = 0f
}

fun main() {
    val i: Int = bunch[Int::class]!!
	val f: Float = bunch[Float::class]!!
    println(i)
    println(f)
}

class MutableTypedMap private constructor(private val map: MutableMap<KClass<*>, Any>) {
  constructor() : this(mutableMapOf())

  @Suppress("UNCHECKED_CAST")
  operator fun <T: Any> get(key: KClass<T>): T? = map[key] as T?

  operator fun <T: Any> set(key: KClass<T>, value: T) {
    map[key] = value
  }
}

```

---

<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:** [May 10, 2025, 10:20am UTC](https://discuss.kotlinlang.org/t/access-value-by-type-with-any-kind-of-data-structure/30348/3 "2025-05-10T10:20:50Z")

</div>

You can also check my little library: [GitHub - broo2s/typedmap: Type-safe heterogeneous map in Kotlin](https://github.com/broo2s/typedmap) , although your case is a small subset of its functionality, so it may be considered an overkill. You can also read the README for alternative libraries or approaches.

```kotlin
// create a typed map
val sess = simpleTypedMap()

// add a User item
sess += User("alice")

// get an item of the User type
val user = sess.get<User>()

println("User: $user")
// User(username=alice)

```

---

<div class="post-metadata">

**Author:** ![kalayda](https://avatars.discourse-cdn.com/v4/letter/k/f05b48/32.png) [@kalayda](https://discuss.kotlinlang.org/u/kalayda)\
**Post date:** [May 10, 2025, 12:21pm UTC](https://discuss.kotlinlang.org/t/access-value-by-type-with-any-kind-of-data-structure/30348/5 "2025-05-10T12:21:15Z")

</div>

@kyay10 @broot Guys, you are great. Thank you!

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [May 11, 2025, 10:53am UTC](https://discuss.kotlinlang.org/t/access-value-by-type-with-any-kind-of-data-structure/30348/6 "2025-05-11T10:53:46Z")

</div>

Also look at the Kotlin coroutine code, specifically the `CoroutineContext`. From what I understand, it’s basically a Map that does exactly what you want; has typed Keys and Values.
