Unrelated question. How is `MutableList` both an i...
# getting-started
h
Unrelated question. How is
MutableList
both an interface and a class in the stdlib without their names clashing? Do they have separate jvmNames?
s
can you link to the doc page for the class? I wasn’t aware that there was duplication here
h
All I know is I can write this:
Copy code
class foo<T>(data: MutableList<T>): MutableList<T> by data {
}
And you can only delegate to interfaces. And I can also write this:
Copy code
val foo = MutableList(10) { it + 2 }
What is going on here? Is the second not an invocation?
s
it looks like a top-level function defined in
Collections.kt
d
Yeah… Here’s the body of the function:
Copy code
public inline fun <T> MutableList(size: Int, init: (index: Int) -> T): MutableList<T> {
    val list = ArrayList<T>(size)
    repeat(size) { index -> list.add(init(index)) }
    return list
}
h
Maybe I just never noticed or does IDEA make class instantiation calls look the same as function calls? How did i get that mixed up?
d
Well without a
new
operator constructor calls are indistinguishable from function calls.
s
on my machine, IDEA renders the
MutableList
invocation in italics, much like static methods in java or like extension functions (without the orange text color anyhow)
h
I want to avoid using
open
classes whenever possible, so I'm trying to use delegation to implement wrapper classes succinctly.
same here. i guess i just conflated them
I should study how MutableList is implemented then, because it certainly ACTS like a class instance
a
Behind the scenes, when you create a kotlin list, it creates a java collection. The kotlin interfaces is how we wind up with List and MutableList.
h
Yeah i think
MutableList
is just an extension of Java's
ArrayList
with the kotlin stdlib collections functions added
it makes me wonder how it's implemented in K/N
a
Here's a quote from the JetBrains course: "_Under the hood, both these interfaces are compiled to java.util.List, to the same interface. And also by default when you create an instance of ArrayList, java.util.ArrayList will be created and Kotlin somehow pretends that java.util.ArrayList implements Kotlin MutableList. There is some kind of compiler magic when a Java interface is substituted by Kotlin interface._"
I'd love to look at the source but have no idea where to find it.
e
Just Ctrl+Click in IDEA and you’ll get to the source.
a
@elizarov all I see when I do that is the interface.