Revised RFC on range design for D2

Sean Kelly sean at invisibleduck.org
Fri Sep 12 16:28:34 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've found this invaluable at times.  And it's actually supported by C++ 
containers.  Something along the lines of:

     for( auto i = c.begin(); i != c.end(); )
     {
         if( shouldRemove( *i ) )
             i = c.remove( i );
         else
             ++i;
     }

In fact, it may not even be possible to remove elements of a container 
outside the inspection loop.  For example, let's say the container is 
actually a SQL resultset and the iterator is a cursor.  It's easy to 
mark a row as 'deleted' in this instance, but storing information to 
perform a deletion later often simply can't be done.  Though I suppose 
you could argue that the 'container' abstraction may be inappropriate 
for such temporal data.


Sean


More information about the Digitalmars-d-announce mailing list