Hello all ```class MapBuilder<T,U> { ope...
# getting-started
d
Hello all
Copy code
class MapBuilder<T,U> {
    operator fun invoke(arg: T): MapBuilder<T, U> {
        return this
    }
    operator fun invoke(arg: U): MapBuilder<T, U> {
        return this
    }
}
of course it's don't work due to JVM limitations. Platform declaration clash: The following declarations have the same JVM signature
Copy code
(invoke(Ljava/lang/Object;)Lcom/test/tests/MapBuilder;):
    operator fun invoke(arg: T): MapBuilder<T, U> defined in com.test.tests.MapBuilder
    operator fun invoke(arg: U): MapBuilder<T, U> defined in com.test.tests.MapBuilder
Any ideas how can I realize this?
b
That's not a JVM limitation. For example, what happens if T == String and U == String?
i
You can use
@JvmName
annotation to overcome jvm declaration clash
b
@ilya.gorbunov Ahh, I forgot about reification and partial specialization in Kotlin. Thanks for pointing that out!
a
Just guessing but based on the map, is this what you wanted?
Copy code
class MapBuilder<K, V> {
    val pairs: MutableList<Pair<K, V>> = mutableListOf()

    inner class EntryBuilder constructor(val key: K) {
        operator fun invoke(value: V): MapBuilder<K, V> {
            pairs += key to value
            return this@MapBuilder
        }
    }

    operator fun invoke(key: K) = EntryBuilder(key)
}