Revised RFC on range design for D2

Steven Schveighoffer schveiguy at yahoo.com
Fri Sep 12 13:54:12 PDT 2008


"Walter Bright" wrote
> Andrei Alexandrescu wrote:
>> Ary Borenszweig wrote:
>>> - Is it possible to add elements to a range? Suppose a linked list, you 
>>> want to traverse it's elements until a condition is met on an element, 
>>> and then add something after it. Or before it. I see there's "put", but 
>>> it also says "An output range models a one-pass forward writing 
>>> iteration.", so I think it's not the same.
>>
>> Never. Ranges never grow. You need access to the "mother" container, 
>> which will offer primitives for insertion and removal of elements.
>
> I agree. I don't think it is ever a good idea to try to add/remove 
> elements of a container while iterating over it. foreach disallows it.

I allow removal during foreach in dcollections.  However, it is done by the 
opApply function at the request of the delegate (I pass in a ref boolean).

I don't think this method violates the foreach contract.

I use this when I'm iterating over a container trying to decide whether 
elements should be removed.  If you don't allow this, then either you must 
use an iterator/range model, or build a separate list of elements to delete 
once you have exited the foreach loop.

Using this model, I can do things like O(n) traverse and remove from an 
array.

-Steve 




More information about the Digitalmars-d-announce mailing list