Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 18, 2011 Init const fixed-sixed array in ctor | ||||
---|---|---|---|---|
| ||||
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() {} Bye, bearophile |
June 18, 2011 Re: Init const fixed-sixed array in ctor | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
|
June 18, 2011 Re: Init const fixed-sixed array in ctor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | Jonathan M Davis: 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. Don't worry, I have defined the array of length one just because I like to minimize my examples :-) > Now, if > array = [c]; > doesn't work, _then_ we have a problem. This doesn't compile, nor several variants of it: struct Foo { const char[1] array; this(char c) { array = [c]; } } void main() {} Bye, bearophile |
June 18, 2011 Re: Init const fixed-sixed array in ctor | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 2011-06-17 18:18, bearophile wrote:
> Jonathan M Davis:
>
> 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.
>
> Don't worry, I have defined the array of length one just because I like to minimize my examples :-)
>
> > Now, if
> > array = [c];
> > doesn't work, _then_ we have a problem.
>
> This doesn't compile, nor several variants of it:
>
>
> struct Foo {
> const char[1] array;
> this(char c) {
> array = [c];
> }
> }
> void main() {}
Then that's a problem, since that makes initializing the array dynamically impossible. Definitely worthy of a bug report.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation