Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

tango.util.collection.ArraySeq opApply

Moderators: kris

Posted: 03/08/08 22:38:10

I don't know if this is wanted behaviour, but current implementation of opApply in ArraySeq? [which creates object of subclass ArrayIterator?, and by calling opapply implemented in this subclass] disallow modifying elements in foreach loop (from what I've seen probably also in ArrayBag?, and maybe also in other collection classes):

    auto list = new ArraySeq!(int);

    foreach (i; [9, 8, 7, 6, 5, 4, 3, 2, 1])

    foreach (inout i; filtered)
        i += 10;

    foreach (i; list)
        Stdout (i) (", ");

in case of ArraySeq?, this could be hacked like this:

    for (auto i=remaining(); i--;)
            auto value = get();
            if ((result = dg(value)) != 0)
            if (value != array[row-1]) // evil hack
                array[row-1] = value;
     return result;

but I consider this as a hack rather than proper solution.

If this is wanted and desired behaviour, what is motivation behind that?

Author Message

Posted: 03/09/08 07:20:36

The historical reason is that the iterators are "fail fast", meaning they check for mutation within the container, and bail out when that happens. If the iterator were to allow container mutation during traversal, it would effectively kill itself.

That aside - we have a recent version of the containers which do enable mutation during iteration, which will be made available soon.