BruceEckel/AtomicKotlinExamples

p. 130 Lists/MutListIsList.kt: Uncomment "// list += 3 // Error" line did not cause an error

weiqigao opened this issue · 4 comments

Instead of causing an error, the "list += 3" line successfully inserts another 3 into the list. And the eq test on the next line fails:
[1, 2, 3, 3]
[Error]: [1, 2, 3, 3] != [1, 2, 3]

You had to have changed val list = getList() to var list = getList() in order to produce that result.

However, I see the problem. list += 3 is creating a new List and re-assigning that to list. It is confusing, because += is doing some tricky things. add() is probably a better way to demonstrate this.

I did indeed have "var list = getList()" in my file.

I think what I need to do is simplify this example so it uses add() instead of +=.

And then I need to add a section on how += works when using containers.

I just updated the Leapub ebook with the new version of the Lists chapter, with rewrites and a new section describing the behavior of +=.