harry.singh
02/23/2019, 8:20 AMvar immutableList = listOf(1, 2, 3)
immutableList += 4
Since plusAssign
operator is not defined on a Collection
in Kotlin, what does immutableList += 4
really do. It can't add the element to the the List
as it is immutable unlike ArrayList
. So does it first create a new List
from the plus
operator and then assign it to the reference variable through =
?Ruckus
02/23/2019, 8:23 AMimmutableList = immutableList + 4
Note that if this was a mutable list, you'd get an error from ambiguity.harry.singh
02/23/2019, 8:46 AMmutableList = mutableList + 4
and mutableList.plusAssign(4)
would be applicable in this case and hence compiler will generate an error for the ambiguityharry.singh
02/23/2019, 8:46 AMRuckus
02/23/2019, 8:47 AMkarelpeeters
02/23/2019, 8:51 AMharry.singh
02/23/2019, 9:29 AM+
in immutable list example right?karelpeeters
02/23/2019, 9:30 AMharry.singh
02/23/2019, 9:30 AMplus
operator defined on List
, we would never get compiler error in the case of mutable list for +=
right?karelpeeters
02/23/2019, 9:37 AMRuckus
02/23/2019, 3:25 PM