mbonnin
06/22/2022, 3:24 PMSuspiciousCollectionReassignment
inspection. The description says:
fun test() {
var list = listOf(0)
list += 42 // new list is created, variable 'list' still contains only '0'
}
But doing the same thing in playground yields [0, 42]
. What am I missing?Paul Griffith
06/22/2022, 3:27 PM+=
operator is quietly allocating an entire new collectionmbonnin
06/22/2022, 3:28 PMvar
propertiesPaul Griffith
06/22/2022, 3:29 PMmbonnin
06/22/2022, 3:29 PMPaul Griffith
06/22/2022, 3:31 PMlist = list + 42
mbonnin
06/22/2022, 3:32 PMnkiesel
06/22/2022, 8:07 PMvar l1 = listOf(0); var l2 = l1; l1 += 42
results in "l2=[0]", but a var l1 = mutableListOf(0); var l2 = l1; l1 += 42
results in "l2=[0,42]"mbonnin
06/22/2022, 8:09 PMvar l1 = 0; var l2 = l1; l1 += 42
results in "l2==0" so I'd argue this is behaving as expectednkiesel
06/22/2022, 8:11 PMmbonnin
06/22/2022, 8:12 PMl1 += 42
is not the same as l1 = l1 + 42
in the mutable case feels weirdmcpiroman
06/22/2022, 8:44 PM+=
with `var MutableList`but no with `var List`nor val MutableList
.Alexey Belkov [JB]
06/24/2022, 2:19 PM