https://kotlinlang.org logo
Title
h

Hullaballoonatic

04/11/2019, 8:28 PM
Unrelated question. How is
MutableList
both an interface and a class in the stdlib without their names clashing? Do they have separate jvmNames?
s

Shawn

04/11/2019, 8:30 PM
can you link to the doc page for the class? I wasn’t aware that there was duplication here
h

Hullaballoonatic

04/11/2019, 8:32 PM
All I know is I can write this:
class foo<T>(data: MutableList<T>): MutableList<T> by data {
}
And you can only delegate to interfaces. And I can also write this:
val foo = MutableList(10) { it + 2 }
What is going on here? Is the second not an invocation?
s

Shawn

04/11/2019, 8:33 PM
it looks like a top-level function defined in
Collections.kt
d

dalexander

04/11/2019, 8:34 PM
Yeah… Here’s the body of the function:
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

Hullaballoonatic

04/11/2019, 8:34 PM
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

dalexander

04/11/2019, 8:35 PM
Well without a
new
operator constructor calls are indistinguishable from function calls.
s

Shawn

04/11/2019, 8:36 PM
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

Hullaballoonatic

04/11/2019, 8:36 PM
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

Al Warren

04/11/2019, 10:18 PM
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

Hullaballoonatic

04/11/2019, 10:20 PM
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

Al Warren

04/12/2019, 1:08 AM
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

elizarov

04/13/2019, 7:49 AM
Just Ctrl+Click in IDEA and you’ll get to the source.
a

Al Warren

04/14/2019, 3:27 PM
@elizarov all I see when I do that is the interface.