for, foreach identifier allowed in c throws error in d

Logesh Pillay via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 2 12:21:06 PDT 2014


It is common in a recursive function to amend a global array 
using the function parameter to refer to the element eg

int[10];

void foo (int i) {
   foreach (x; 0 .. 9) {
      t[i] = x;
      foo ();

C in a for loop allows use of t[i] directly as the iterating 
variable so you don't need the dummy variable x which has no real 
function.

D does not.  The error generated is "no identifier for declarator 
t[i]". For a long time, I thought that was specific to foreach 
but the same thing happens with for.

It looks like an unnecessary restriction.  Am I missing something?


More information about the Digitalmars-d-learn mailing list