0

I'm practicing with collections in Kotlin and cannot understand behavior of interfaces. Here is background of my question:

public interface List<out E> : Collection<E> {...}
public interface MutableList<E> : List<E>, MutableCollection<E> {...}

List and MutableList are interfaces defined in Kotlin, which is easy to understand. Now if I want to get an instance of a List these methods can be used: arrayListOf() or mutableListOf(). Both methods use the same typealias, which is actually links to the Java implementation of ArrayList.

public typealias ArrayList<E> = java.util.ArrayList<E>

Here a mysterious thing, MutableList doesn't have an implementation in Kotlin, so how Kotlin knows that java.util.ArrayList can be assigned to a MutableList variable? In other words, how Kotlin and Java interfaces are correlated to each other?

1 Answer 1

2

Interfaces in Kotlin have a one-to-one relationship with Java interfaces. MutableList and other collection interfaces are an exception to this; they are handled by the compiler in a special way to support the separation between read-only and writable collection interfaces.

Sign up to request clarification or add additional context in comments.

6 Comments

Are any good resources over the Internet to read about the compiler internals? Especially related to interfaces processing.
The only resource covering the internals would be the compiler source code.
thx for the link! I was afraid that the source code is in C but hopefully it's not:)
The Kotlin compiler is a JVM application. Why would its source code be in C?
Does IntArray handled by the compiler internally? It source code looks more like an interface.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.