https://kotlinlang.org logo
Title
s

sreich

08/12/2022, 5:09 PM
is there a way to resolve an ambiguous += to a list?
cliOptions += CliOption()
clioptions is a java List
Assignment operators ambiguity. All these functions match.
public operator fun <T> Collection<CliOption!>.plus(element: CliOption!): List<CliOption!> defined in kotlin.collections
public inline operator fun <T> MutableCollection<in CliOption>.plusAssign(element: CliOption): Unit defined in kotlin.collections
j

Joffrey

08/12/2022, 5:18 PM
You could do
cliOptions.add(CliOption())
or
cliOptions = cliOptions + CliOption()
depending on the one you want
e

ephemient

08/12/2022, 5:40 PM
not always but often
var : MutableList
is a poor design choice anyway
s

sreich

08/12/2022, 5:47 PM
@Joffrey why do i have to do
= +
? this doesn't really answer why += doesn't work, and why i have to explicitly call assignPlus()
which sucks because they're all noisier than just doing +=
e

ephemient

08/12/2022, 5:49 PM
.plusAssign
and
= .plus
are the respective meanings of
+=
with
val cliOptions: MutableList
and
var cliOptions: List
respectively. but if you have a
var cliOptions: MutableList
then
+=
is ambiguous