Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 02, 2016 Is the following code legal? | ||||
---|---|---|---|---|
| ||||
int[int] hash; .. foreach( key, ref value; hash ) { if( value>12 ) hash.remove(key); } Some hash implementations support this, some don't. The D documentation (https://dlang.org/spec/hash-map.html) leaves this not defined. As reference, C++ does define this (in C++ it is allowed, at least since C++14: http://en.cppreference.com/w/cpp/container/unordered_map/erase) Shachar |
November 02, 2016 Re: Is the following code legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shachar Shemesh | On 11/02/2016 10:21 AM, Shachar Shemesh wrote:
> int[int] hash;
>
> ..
>
> foreach( key, ref value; hash ) {
> if( value>12 )
> hash.remove(key);
> }
>
> Some hash implementations support this, some don't. The D documentation
> (https://dlang.org/spec/hash-map.html) leaves this not defined.
>
> As reference, C++ does define this (in C++ it is allowed, at least since
> C++14: http://en.cppreference.com/w/cpp/container/unordered_map/erase)
>
> Shachar
We should render it defined, and document it as such. Could you please create an issue and I'll have someone look at it. Thanks! -- Andrei
|
November 02, 2016 Re: Is the following code legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shachar Shemesh | 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." |
November 02, 2016 Re: Is the following code legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 11/02/2016 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."
Yah, we'd do good to relax that to allow removal of the currently iterated element. -- Andrei
|
November 03, 2016 Re: Is the following code legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 02/11/16 16:52, Andrei Alexandrescu wrote: > We should render it defined, and document it as such. Could you please > create an issue and I'll have someone look at it. Thanks! -- Andrei https://issues.dlang.org/show_bug.cgi?id=16659 |
November 03, 2016 Re: Is the following code legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 |
Copyright © 1999-2021 by the D Language Foundation