Is the following code legal?

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Nov 3 06:31:20 PDT 2016


On 11/2/16 11:17 AM, Adam D. Ruppe wrote:
> On Wednesday, 2 November 2016 at 14:21:32 UTC, Shachar Shemesh wrote:
>> The D documentation (https://dlang.org/spec/hash-map.html) leaves this
>> not defined.
>
> The foreach statement is defined to not allow it:
>
> http://dlang.org/spec/statement.html#ForeachStatement
>
> "The aggregate must be loop invariant, meaning that elements to the
> aggregate cannot be added or removed from it in the
> NoScopeNonEmptyStatement."

That's only for builtins. Obviously, there are cases where it can work, 
and it needs to be defined by the aggregate/range. I think the 
documentation should be updated to reflect that.

To answer the original question, it's not valid with the current 
implementation AFAIK. I don't think we should define ever that it is 
valid, even if we have an implementation that supports it, as this 
restricts our implementation to always supporting it.

I'll point at my dcollections library as an example where the currently 
iterated value can be removed:

https://github.com/schveiguy/dcollections/blob/master/concepts.txt#L81

I think Java also allows this, and C++ allows this.

-Steve


More information about the Digitalmars-d mailing list