https://kotlinlang.org logo
#stdlib
Title
p

Pere Casafont

04/29/2019, 9:44 AM
Is there a way to get a use of
MutableCollection::plusAssign(Iterable)
to compile without having assignment operators ambiguity error? Here an example that does not compile:
Copy code
val list = listOf(1, 2, 3)
	var mutableList = mutableListOf(4, 5, 6)
	mutableList += list
g

gildor

04/29/2019, 9:49 AM
Only if you replace
var
with
val
If you need
var
there, than use addAll instead:
mutableList.addAll(list)
p

Pere Casafont

04/29/2019, 10:07 AM
oh, yes I need var in my case, and now I understand what's up; thanks!
however, maybe it'd be nice if
plusAssign
had preference over
plus
+
assign
to avoid this odd situation
g

gildor

04/29/2019, 10:13 AM
mutableList += list
is acutally reassign mutableList, but (mutableList + list) returns List, so type is not compatible, so it’s not clear how you can solve it
4 Views