Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
weiqigao opened this issue Nov 18, 2021 · 4 comments

Comments

@weiqigao
Copy link

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]

@BruceEckel
Copy link
Owner

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.

@weiqigao
Copy link
Author

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

@BruceEckel
Copy link
Owner

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.

@BruceEckel
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants