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

Is Set.remove safe to call when iterating? #3543

Closed
peter-ahe-google opened this issue Jun 12, 2012 · 4 comments
Closed

Is Set.remove safe to call when iterating? #3543

peter-ahe-google opened this issue Jun 12, 2012 · 4 comments
Assignees
Labels
area-library type-documentation A request to add or improve documentation

Comments

@peter-ahe-google
Copy link
Contributor

The documentation of Set.remove is:

  /**
   * Removes [value] from the set. Returns true if [value] was
   * in the set. Returns false otherwise. The method has no effect
   * if [value] value was not in the set.
   */
  bool remove(E value);

It does not say whether or not it is safe to call when iterating over the set, for example:

var s = new Set.from(['x', 'y']);
for (var e in s) {
  s.remove('y');
}

Since the documentation does not explicitly state that this is a problem, one could reasonably assume that it is safe to remove elements while iterating.

On the other hand, if it is not safe, then I think the set should have a fail-fast behavior so developers don't experience random behavior that might arise from an iterator getting confused about removed elements.

@lrhn
Copy link
Member

lrhn commented Jun 12, 2012

It's generally not safe to change the originating collection during iteration (or during a call to forEach).
There is currently no safeguards, though. I agree that it should either be mentioned or checked, and preferably checked, so using an iterator after a modification will throw.


Set owner to @lrhn.
Removed Type-Defect label.
Added Type-Enhancement, Accepted labels.

@peter-ahe-google
Copy link
Contributor Author

I don't know what "type-defect" vs. "type-enhancement" means. However, if this is not addressed before we ship the first release, it may be impossible to change.

Deferring this issue means accepting the current behavior and specification. That is, removing elements while iterating is safe.

@andersjohnsen
Copy link

Today an exception will be thrown if elements are removed while iterating:

  Unhandled exception:
  Concurrent modification during iteration: ...

However, it's yet to be specified in the documentation that this is the behavior.


Removed Type-Enhancement label.
Added Type-Documentation label.

@lrhn
Copy link
Member

lrhn commented Jan 5, 2015

It is documented on the Iterable class that you should not modify the iterable while iterating. It's documented on Set, List and Map which operations are considered iteration breaking (changing values of a list or map is not).


Added Fixed label.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-library type-documentation A request to add or improve documentation
Projects
None yet
Development

No branches or pull requests

3 participants