# Inconsistent type parameters : How to implements generic interfaces with differents types

**URL:** <https://discuss.kotlinlang.org/t/inconsistent-type-parameters-how-to-implements-generic-interfaces-with-differents-types/16868>\
**Category:** Support\
**Created:** [March 27, 2020, 5:30pm UTC](https://discuss.kotlinlang.org/t/inconsistent-type-parameters-how-to-implements-generic-interfaces-with-differents-types/16868 "2020-03-27T17:30:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lrouquette](https://avatars.discourse-cdn.com/v4/letter/l/d6d6ee/32.png) [@lrouquette](https://discuss.kotlinlang.org/u/lrouquette)\
**Post date:** [March 27, 2020, 5:30pm UTC](https://discuss.kotlinlang.org/t/inconsistent-type-parameters-how-to-implements-generic-interfaces-with-differents-types/16868/1 "2020-03-27T17:30:34Z")

</div>

Hello Kotliners, I face a problem with type erasure, is there a way to implements implementation of same generic interface with different types ?

```
fun main() {
   val foo = Foo()
   println(foo[0])
   println(foo[0L])
}

interface Index<Idx, Type> {
   operator fun get(i: Idx): Type
}

interface IntIndex<Type> {
   operator fun get(i: Int): Type
} 

interface LongIndex<Type> {
   operator fun get(i: Long): Type
}

// class Foo: Index<Int, String>, Index<Long, String> <- This is forbidden
class Foo: IntIndex<String>, LongIndex<String> { // This is allowed
   override fun get(i: Int) = "TODO for Int"
   override fun get(i: Long) = "TODO for Long"
}

```

---

<div class="post-metadata">

**Author:** ![overmind](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/overmind/32/7272_2.png) [@overmind](https://discuss.kotlinlang.org/u/overmind)\
**Post date:** [March 28, 2020, 3:42pm UTC](https://discuss.kotlinlang.org/t/inconsistent-type-parameters-how-to-implements-generic-interfaces-with-differents-types/16868/2 "2020-03-28T15:42:40Z")

</div>

Sounds like a JVM restriction – both `Index<Int, String>` and `Index<Long, String>` represent the same nominal type. So a supertype declaration can only contain one of them; Whereas `IntIndex` and `LongIndex` are treated as different nominal types.

---

<div class="post-metadata">

**Author:** ![lrouquette](https://avatars.discourse-cdn.com/v4/letter/l/d6d6ee/32.png) [@lrouquette](https://discuss.kotlinlang.org/u/lrouquette)\
**Post date:** [April 3, 2020, 4:59pm UTC](https://discuss.kotlinlang.org/t/inconsistent-type-parameters-how-to-implements-generic-interfaces-with-differents-types/16868/3 "2020-04-03T16:59:10Z")

</div>

Thanks for the response 🙂. Anyone knows if the Valhalla project will “fix” this problem ?

---

<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:** [April 3, 2020, 5:19pm UTC](https://discuss.kotlinlang.org/t/inconsistent-type-parameters-how-to-implements-generic-interfaces-with-differents-types/16868/4 "2020-04-03T17:19:11Z")

</div>

No, it won’t. Valhalla has nothing to do with generics. Actually, in Kotlin you can work around type erasure if you are using compile-time dispatch. Like this:

```kotlin
fun doSomething(list: List<Double>){}
@JvmName("doSomethingWithList")
fun doSomething(list: List<Int>){}

fun main(){
    doSomething(listOf(1,2,3))
}

```

---

<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:** [April 3, 2020, 6:07pm UTC](https://discuss.kotlinlang.org/t/inconsistent-type-parameters-how-to-implements-generic-interfaces-with-differents-types/16868/5 "2020-04-03T18:07:45Z")

</div>

Correction. Part of Valhalla IS about generics, especially reified generics for value-types. But it is not about type erasure.

---

<div class="post-metadata">

**Author:** ![lrouquette](https://avatars.discourse-cdn.com/v4/letter/l/d6d6ee/32.png) [@lrouquette](https://discuss.kotlinlang.org/u/lrouquette)\
**Post date:** [April 3, 2020, 6:45pm UTC](https://discuss.kotlinlang.org/t/inconsistent-type-parameters-how-to-implements-generic-interfaces-with-differents-types/16868/6 "2020-04-03T18:45:30Z")

</div>

Yes that’s why I believe that it would be possible to implements two types in Kotlin as the compiler can perform non valid Java dispatch. By the way thanks for the response
