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

KMutableIterator.remove() doesn't remove item from collection #5

Closed
passsy opened this issue Dec 2, 2018 · 2 comments · Fixed by #198
Closed

KMutableIterator.remove() doesn't remove item from collection #5

passsy opened this issue Dec 2, 2018 · 2 comments · Fixed by #198
Labels
bug Something isn't working collection

Comments

@passsy
Copy link
Owner

passsy commented Dec 2, 2018

var abc = mutableListOf(["a", "b", "c"]);
final KMutableIterator<String> i = abc.iterator();
assert(i.next() == "a");
i.remove();
assert(abc[0] == "b"); // Failed assertion: 'abc[0] == "b"': is not true.

abc[0] is still "a" because remove doesn't remove the item from the actual collection. Instead, it gets removed from the internal copy inside the iterator.

To fix this problem a big refactoring is required so that remove actually removed the item from the original collection.

This is how kotlin solves this

@passsy passsy added the bug Something isn't working label Dec 2, 2018
passsy added a commit that referenced this issue Dec 2, 2018
Prevents #5 from returning wrong results
@susonthapa
Copy link

For the time being, can you suggest any workaround?

@susonthapa
Copy link

susonthapa commented Nov 15, 2021

I am currently using this, and it's working fine. Don't know if this is the best approach to do.

final list = List<T>.from(KtList<T>);
list.removeWhere((it) => it.id == id);
final updatedList = list.toImmutableList();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working collection
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants