On Friday, July 13, 2012 12:15:33 Tommi wrote: > The following code doesn't compile. It seems like a compiler bug > to me, but I can't find a bug report about it. Is it a bug? > > private immutable(int[]) constants; > > shared static this() > { > constants.length = 10; > constants[0] = 123; // Error: constants[0] isn't mutable > }
It's not a bug. You have to initialize the entire array at once, since it's immutable. By setting the length, you initialized the array such that it had 10 elements in it all of which were the init value of the element type (which would be 0, since it's int). If you don't want to use an array litera like constants = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; then create a mutable array which you then either idup and assign it to constants, or you cast it to immutable (or use std.exception.assumeUnique, which casts to immutable but is used to document the fact it is the only reference to that data and therefore safe to cast to immutable) and then assign it to constants. - Jonathan M Davis