www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24232] New: ref for index of foreach for arrays is not

https://issues.dlang.org/show_bug.cgi?id=24232

          Issue ID: 24232
           Summary: ref for index of foreach for arrays is not allowed by
                    spec but accepted by compiler.
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: james.gray remss.net

According to
https://dlang.org/spec/statement.html#foreach_over_arrays
point 2, "ref" on "index" is not allowed. However,
the following compiles:

import std;
void main() {
  foreach(ref i, x; [1,2,3,4,5]) {
    writeln(x);
    i++;
  }
}

and produces:
1
3
5

--
Nov 07 2023