digitalmars.D.learn - Why does foreach allow ref element for non-ref front() of
- Boris-Barboris (16/16) Jan 26 2020 import std;
import std; import std.range; void main() { int[] a = [3, 5, 7]; foreach (i, ref ae; a.enumerate) { writeln(i, " ", ae); ae = 6; } writeln(a); assert(a[].equal([6, 6, 6])); // fails, a is in initial state } Why does the compiler allow such 'ref ae' loop, if the actual value is not a reference? Or it is a reference to hidden stack variable that was copied by enumerate from a during front()?
Jan 26 2020