On 5/17/13 2:09 AM, David Miguel Antunes wrote:
> Well, yes it is possible that there is no bug.
>
> I was having very strange random behavior in the application, and it
> seemed that the listeners weren't always being called.
>
> I though it may have to be with the functions changing their hash on
> some situations, in which case it would be similar to the bug of having
> objects in sets or hashmaps and changing their hashCode value:
>
http://stackoverflow.com/questions/5110376/hashset-contains-problem-with-custom-objects
>
> When I changed to a ListBuffer, all seemed to be ok.
>
> Probably I was inserting new listeners into the set, while iterating
> with foreach. I don't know what is the expected behavior in this
> situation (I think in Java it crashes), but I suppose the iteration
> would go wrong silently.
Depends what you're using in Java. With a synchronizedSet it will throw
a ConcurrentModificationException if you mutate the collection in the
midst of the iteration. Even in a single-threaded program. With other
types of collections, you'll probably get to iterate over the newly
added elements as well.
In general it's a pretty bad idea to mutate the collection you're
iterating over. Some would go as far as saying that's a pretty bad idea
to mutate :)
Not sure what's different in the implementations of mutable.Set and
ListBuffer in Scala. The hierarchy is too large to dig in right now.
> With a ListBuffer, even if I appended new listeners, the iteration of
> the existing ones probably continues as expected, this is probably why
> the ListBuffer seemed to fix it.
> (Right now I create a copy and iterate on the copy)
>
> P.S.:
> No bug, but still: candies for everyone :)
> <mailto:
scala-user%2Bunsu...@googlegroups.com>.
--
Ionuț G. Stan |
http://igstan.ro