The following is legal
proc advance(dt) {
param n = numBodies-1;
for param i in 0..n {
const pi = bodies[i].pos; // rip 'pos' out of loop
const mi = bodies[i].mass; // rip 'mass' out of loop
var vi = bodies[i].vel; // accumulate within loop
for param j in i+1..n {
ref bj = bodies[j];
const dp = pi - bj.pos;
const dpsq = sumOfSquares(dp);
const mag = dt / (dpsq * sqrt(dpsq));
vi -= dp * bj.mass * mag; // line 111
bj.vel += dp * mi * mag; // line 112
}
bodies[i].vel = vi; // line 114
bodies[i].pos = pi + vi * dt; // line 115
}
}
Change the for param to foreach and it is illegal:
chpl --fast n4.chpl
n4.chpl:97: In function 'advance':
n4.chpl:111: error: cannot assign to const variable
n4.chpl:105: note: The shadow variable 'vi' is constant due to task intents in this loop
n4.chpl:112: error: cannot assign to const variable
n4.chpl:114: error: cannot assign to const variable
n4.chpl:100: note: The shadow variable 'bodies' is constant due to task intents in this loop
n4.chpl:115: error: cannot assign to const variable
n4.chpl:100: note: The shadow variable 'bodies' is constant due to task intents in this loop
n4.chpl:77: called as advance(dt: real(64))
What am I doing wrong?