Comment #0 by bearophile_hugs — 2011-10-08T19:17:03Z
Maybe this was reported already, because it looks like a basic bug.
This compiles and runs with no errors (DMD 2.056head):
void main() {
enum int[][] array = [[0, 1], [2, 3]];
foreach (r; array)
r[0] *= 10;
foreach (ref r; array)
r[0] *= 10;
assert(array == [[0, 1], [2, 3]]);
}
This is a big hole. I think the implementation of compile-time constants need a lot of debugging.
Comment #1 by lovelydear — 2012-05-01T15:08:48Z
This simpler case actually fails:
void main() {
enum int[] array = [2, 1];
foreach (r; array){
r *= 10; // should be rejected
writeln(array);
}
foreach (ref r; array){
r *= 10; // should be rejected
writeln(array);
}
assert(array == [20, 10]);
}
Changing enum to immutable gives the following compilation errors:
bug.d(8): Error: variable bug.main.r cannot modify immutable
bug.d(12): Error: variable bug.main.r cannot modify const
But this
void main() {
enum int[] array = [2, 1];
array[0] = 5;
}
is correctly caught by the compiler:
bug.d(7): Error: constant [2,1][0] is not an lvalue
Comment #2 by andrej.mitrovich — 2012-12-02T10:33:38Z
There is no "hole" here, what you get here are separate arrays which are allocated for you wherever you use them. For example:
import std.stdio;
void main()
{
enum int[] array = [1, 2];
foreach (i, _; array)
writeln(&array[i]);
foreach (i, ref _; array)
writeln(&array[i]);
}
Prints:
972FE0
972FD4
972FB0
972FA4
Those are two separate arrays.