I\'m implementing a stack algorithm for study purpose in Kotlin
class Stack>(list:MutableList) {
var items: Mutabl
listOf is not truly mutable. According to the doc:
funReturns a new read-only list of given elements. The returned list is serializable (JVM).listOf(vararg elements: T): List (source)
You should use mutableListOf<> instead.
The reason why as MutableList is permitted here is because listOf(10) returns Collections.singletonList(10) which returns a java.util.List (which Kotlin assumes implements the kotlin.collections.MutableList interface). So the compiler does not know it's not really mutable until the mutating method is called at runtime and throws the exception.