foreach ref very broken: fails to call front(val)

monarch_dodra monarch_dodra at gmail.com
Wed Jul 11 04:19:38 PDT 2012


Giving Array an opApply operator fixes both problems:

1) It allows modifying the elements when accessed with "ref".
2) It allows writing "++a" even though "++front" isn't supported.

I implemented this and made a pull request:
https://github.com/D-Programming-Language/phobos/pull/683

I implemented both opApply and opApplyReverse.
I implemented it both for Array.Range and Array (see: 
http://forum.dlang.org/thread/rxpbtrawpjzvdfuuwmwp@forum.dlang.org)
I implemented correct behavior for "break" cases.
I documented the public opApply.
I wrote a complete unit test.
I tested && unittested it.

This is my first time doing this, so please forgive me if I did 
something wrong :(

----
If this gets validated, then maybe the same should be done to the 
other containers? AFAIK, none of them work correctly with 
foreach(ref).

----
Note that i did not touch Array!bool. This is because in that 
case, I cannot, even internally, pass a bool by reference to the 
delegate.

A workaround would be to read a bool into a temporary, pass 
temporary to the delegate, write back to container. However, I do 
not know how to distinguish the case where the delegate modifies 
the bool or not, making the general case too expensive.

I preferred not touching it, and doing things one at a time.
----

Thank you all for the time you have already invested in this 
thread (as well as the others...)


More information about the Digitalmars-d mailing list