You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/**
* 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.
The text was updated successfully, but these errors were encountered:
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.
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.
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).
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.
The text was updated successfully, but these errors were encountered: