Kotlin is a fully object-oriented language in which functions are objects and basic types are objects, so Kotlin’s types behave with a high degree of consistency in a type system similar to Java’s nonconforming ones.
This is good, and not only Kotlin does it, but many languages do it. However, Kotlin claims to be 100% Java compatible. If anything goes wrong, it’s Kotlin’s fault.
A cute new member of our community asked about a Java interface:
interface IntMap<V> extends java.util.Map<Integer, V> {
V get(Integer key);
V get(int key);
}Copy the code
You can’t do that with Kotlin.
When I first heard about it, I said it was happening! After waiting for the home to try to find, really can not achieve!
Why? Since there is no basic type for int in Kotlin, the int type will automatically choose between an Integer and an int mapped to Java bytecode when compiled, depending on the situation, so we don’t have to care or decide what int will compile into.
So when Kotlin implements this interface, the two get signatures become consistent:
class IntMapImpl: IntMap<String> { override fun get(key: Int?) : String { ... } override fun get(key: Int): String { ... }
}Copy the code
I have to say, it’s a sad story. It was the first problem I ever encountered that I didn’t know how to solve, because Kotlin’s compiler actually wrote it.
If you are interested in joining us, please follow Kotlin directly or add QQ group: 162452394 to contact us.