Init const fixed-sixed array in ctor

Jonathan M Davis jmdavisProg at gmx.com
Fri Jun 17 18:01:42 PDT 2011


On 2011-06-17 17:09, bearophile wrote:
> This is a known bug, right?
> 
> 
> struct Foo {
>     const char cc;
>     const char[1] array;
>     this(char c) {
>         cc = c;       // OK
>         array[0] = c; // Error: this.array[0] isn't mutable
>     }
> }
> void main() {}

You probably have to set the whole array at once. The array's elements _are_ 
const after all. In every other place that you want to initialize a const or 
immutable array, you have to do it all at once. I don't see why this would be 
any different. Sure, it's a special case where setting that one element sets 
the whole array, but making that work would be special casing for such an 
array and complicate the compiler for little benefit. Now, if

array = [c];

doesn't work, _then_ we have a problem.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list