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
ArrayIndexOutOfBoundsException when adding elements to a ParHashMap while iterating #9448
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9448?orig=1 |
Samuel Gélineau (gelisam) said (edited on Aug 26, 2015 2:02:16 AM UTC):
Ah, my mistake then. Is this documented anywhere? I assumed that mutating collections during iteration was allowed in Scala because before filing the bug,
Note that even if I had found the sentence "dealing with mutable collection means you need to understand which code changes which collection when" in the Collections overview, I would have interpreted this as meaning that I need to understand which part of my code changes a collection, that is, that I'll experience the usual pain of imperative programming based on shared state. |
@Ichoran said: I think the general principle for mutable collections in Scala is, as Jason says, that mutation cannot safely occur during traversal (whether you have an iterator or no). It would be great for the docs to be much clearer about what is and is not okay. Intuitively it probably seems kind of sketchy to use transform inside transform, so people probably don't do it, but simple traversal and addition? It often seems okay in the simplest cases, but breaks when things get more involved. For example, consider val m = collection.mutable.HashSet((1 to 10): _*)
m.foreach{ i => m += i+100; m += i+200; m += i+300; m -= i+1 } If you try simpler variations of this, it looks like it magically all works: e.g. if you just have the removal, every second of the original indices is removed. But with this you get gobbledygook, as the foreach is just running down the original array, and slots appear and disappear willy-nilly, and then the array is resized to be larger partway through which isn't reflected in the foreach! So, bottom line is: good documentation for what happens would be awesome. But don't put mutation inside of map & friends, whether the collections are parallel or sequential. |
ParHashMap non-deterministlically throws an exception, but HashMap doesn't.
The "while (true)" is to try repeatedly until it fails, as the failure is non-deterministic.
The text was updated successfully, but these errors were encountered: