Bug 14625 – opIndex() doesn't work on foreach container iteration

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-05-28T03:57:00Z
Last change time
2015-08-29T09:32:48Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
k.hara.pg

Comments

Comment #0 by k.hara.pg — 2015-05-28T03:57:02Z
A non-range container object iteration `foreach (e; c)` is implicitly converted to a range iteration, by application of implicit slicing like as `foreach (e; c[])`. (Maybe the feature is not well documented in website? http://dlang.org/statement#foreach-with-ranges ) struct Range { @property bool empty() { return true; } @property int front() { return 0; } void popFront() {} } struct Container { Range opSlice() { return Range(); } } void main() { Container c; foreach (e; c) {} } On the other hand, new integrated array operator overloading says as follows: http://dlang.org/operatoroverloading#Slice > To overload a[], simply define opIndex with no parameters: But, it does not work on the container iteration. Test case is: // Range struct is same as above struct Container { Range opIndex() { return Range(); } Range opSlice() { assert(0); } } void main() { Container c; //foreach (e; c) {} // asserts in opSlice(), NG foreach (e; c[]) {} // calls c.opIndex(), OK } The orignal post in d.learn forum: http://forum.dlang.org/thread/[email protected]
Comment #1 by k.hara.pg — 2015-05-28T15:13:09Z
Comment #2 by github-bugzilla — 2015-08-29T07:40:32Z
Commits pushed to stable at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/248805a4ba23278622f6831a172779f3012b1b03 fix Issue 14625 - opIndex() doesn't work on foreach container iteration https://github.com/D-Programming-Language/dmd/commit/d99699301eb0f5d46b4f9240236ca7c1694bb39b Merge pull request #4948 from 9rnsr/fix14621 Issue 14621, 14624, 14625 - Fix bugs in array operator overloading semantics
Comment #3 by github-bugzilla — 2015-08-29T09:32:48Z