Bug 2443 – opApply should allow delegates that are not ref if it makes no sense

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-11-06T09:44:00Z
Last change time
2015-06-09T05:11:54Z
Keywords
patch, spec
Assigned to
nobody
Creator
schveiguy
Blocks
4264

Comments

Comment #0 by schveiguy — 2008-11-06T09:44:44Z
Currently, builtin arrays enjoy the restriction that you cannot ref the index parameter when doing a foreach: int main(string[] args) { foreach(ref i, ref s; args) // Error: foreach: key cannot be out or ref { } } But when trying to do something similar on a struct, the compiler cannot resolve the opApply: struct S { int[] arr; int opApply(int delegate(int i, ref int v) dg) { int result = 0; foreach(i, ref x; arr) { if((result = dg(i, x)) != 0) break; } return result; } } void main() { S s; foreach(i, ref v; s) {} } emits: testit.d(32): function testit.S.opApply (int delegate(int i, ref int v) dg) does not match parameter types (int delegate(ref int __applyArg0, ref int v)) testit.d(32): Error: cannot implicitly convert expression (__foreachbody1) of type int delegate(ref int __applyArg0, ref int v) to int delegate(int i, ref int v) This has implications for const as well, because it would make sense in S above to have an opApply WITHOUT a ref int for the value in the case of a const S.
Comment #1 by code — 2009-12-07T04:52:15Z
This applies to D1 as well.
Comment #2 by k.hara.pg — 2011-12-26T02:05:32Z
Comment #3 by bugzilla — 2011-12-26T16:40:59Z
Comment #4 by github-bugzilla — 2012-03-03T21:25:06Z
Commit pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/f4a653ad068abe8436bac8dedd597533515ed090 Merge pull request #474 from dsimcha/master Remove Bug 2443 Workaround from InputRangeObject
Comment #5 by github-bugzilla — 2012-03-08T22:54:52Z