Map of Kotlin using operators

He started playing on the weekend with Kotlin and tried to get cards that work with operators. Somehow, Kotlin tells me that I am confusing him with ambiguity.

Here is the code that works (syntactically not the way I want):

var columns = sortedMapOf("a" to 1, "b" to 2)
columns.plusAssign("c" to 3)

And here is a code that just doesn't compile (but is syntactically closer to what I want)

var cs = sortedMapOf(1 to "a", 2 to "b")
cs += Pair(3, "c")

What short operational magic / casting am I missing?

Thanks in advance.

+4
source share
1 answer

, cs += Pair(3, "c") , , , cs = cs.plus(Pair(3, "c")) , cs.plusAssign(Pair(3, "c"))

, Kotlin - val, var!

cs val (non-mutable variable), , : plusAssign.

+7

Source: https://habr.com/ru/post/1614023/


All Articles