New linked list

Derek Parnell derek at psych.ward
Wed May 24 00:09:02 PDT 2006


On Wed, 24 May 2006 08:47:21 +0200, Frits van Bommel wrote:

> Boris Wang wrote: [edited upside-down post]
>> "Walter Bright" <newshound at digitalmars.com> 写入消息新闻:e3vq3l$1rp5$3 at digitaldaemon.com...
>>> Sean Kelly wrote:
>>>> Very cool.  One thing... does this work:
>>>>
>>>>     foreach( Person p; per.each )
>>>>     {
>>>>         if( p.age > 50 )
>>>>             p.listRemove();
>>>>     }
>>>>
>>>> ie. can you remove elements within a foreach?
>>> It's undefined behavior. foreach is entitled to assume that the aggregate 
>>> is a loop invariant, although the contents of the aggregate's elements can 
>>> change. This precludes things like resizing an array inside a foreach, 
>>> etc. 
>> 
>  > why not make a feature:
>  >
>  >     safe foreach( Person p; ....)
> 
> If such a feature was to be implemented, I can think of a better 
> (cleaner and more consistent with the rest of the language) syntax:
> 
>      foreach(Peron p; inout people)
>      {
>          // ...
>      }
> 
> Why invent a new keyword when an old one (though in a different place) 
> perfectly expresses what's going on?

Yes! 

The meaning of this would be that "people" would be 're-evaluated' prior to
each iteration under the assumption that something in the body of the loop
modified the array in some manner.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
24/05/2006 5:07:14 PM



More information about the Digitalmars-d-announce mailing list