www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16659] New: Clarify mutating while iterating rules

https://issues.dlang.org/show_bug.cgi?id=16659

          Issue ID: 16659
           Summary: Clarify mutating while iterating rules
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dlang.org
          Assignee: nobody puremagic.com
          Reporter: shachar shemesh.biz

Consider the following program:
int[int] hash;

..

foreach( key, ref value; hash ) {
    if( value>12 )
        hash.remove(key);
}

There is no reference in https://dlang.org/spec/hash-map.html whether this is
legal or not (i.e. - removing an element from the hash while iterating it).

According to http://dlang.org/spec/statement.html#ForeachStatement, this seems
forbidden (in which case it needs to be documented in the first link), but a
forum discussion seemed to gravitate toward needing to allow this (as C++ does,
at least since C++14).

--
Nov 03 2016