Hi, I have a question: Val list = listOf("abc,"tff...
# android
p
Hi, I have a question: Val list = listOf("abc,"tff") printf("list is of type"+ list is MutableList<*>) It returns true. Why does it return true? shouldn't it be false
s
Hmmm, it should be False actually because listOf() creates an immutable list
🚫 1
p
Kotlin repl and kotlang browser return true 😭
l
The underlying type of listOf is not actually specified. It could be an ArrayList, or other list (a quick look at a Kotlin/JVM project on my laptop shows it's SingletonList, which is mutable). The method returns an instance of the List interface, which does not guarantee that the list is mutable, but it can be.
👍 1
Take for example, 'fun foo(): List<Int> = ArrayList<Int>()'. My foo signature only promises that I'll return a list. If you happen to know that it's actually an ArrayList, which is mutable, and don't care if the implementation of foo changes, you can cast to MutableList, since an ArrayList is actually a MutableList.
👍 1
a
Distinguish between read-only and immutable.
List<T>
is read-only, but not (necessarily) immutable.
2
👍 1
s
Interesting..
a
Buzzinga ?