Viktor
Viktor

Reputation: 646

Why 'add' method doesn't work for mutableListOf()?

I have my own converter from Strings to List

object TypeConverter {

    fun stringToListLong(text: String): List<Long> {
        val listLong = mutableListOf<Long>()
        val listString = text.split(",").map { it.trim() }
        listString.forEach {
            listLong.add(it.toLong())
        }
        return listLong
    }
}

Then when I try to use it like below it shows the error(Unresolved reference: add)

val someString = "something"
var ids = TypeConverter.stringToListLong(someString)
ids.add(some long value)

Why?

Upvotes: 0

Views: 383

Answers (1)

ekalin
ekalin

Reputation: 1012

You're returning a List<>, so ids is a List<>, therefore it does not have mutation operations. Make stringToListLong return MutableList<Long>.

Upvotes: 4

Related Questions