September 02, 2010
// Original question by Peter Alexander is at http://stackoverflow.com/ questions/3627023/how-do-you-initialise-an-array-of-const-values-in-d2

to summarize consider this code:

const(int)[2] a;
const int [2] b;
const(int)[] c;
const int [] d;

void main () {
  a = [1, 2]; // Error: slice a[] is not mutable
             // - is it a bug or according specification
  b = [1, 2]; // Error: slice b[] is not mutable - this is ok
  c = [1, 2]; // can assign - this is ok
  // d = [1, 2]; // error - cannot assign - this is ok.
}

from high level point of view, there is difference in const(int)[2] and const (int [2]). One would expect that it is possible to rebind b. From low level/implementation point - there seems to be no difference because a and b are value types - there is not indirection.

Possible resolutions:

1. a = [1,2]; should pass and it is a bug in current implementation

2. it is not a bug, then disallow writing const(int)[n] - only full const should be possible to use to prevent confusion.

3. it is not a bug, then update language definition to make static and dynamic arrays constnes modifiers act the same way.

My first question is it a bug or not, then what would be good thing to do...
September 02, 2010
Michal Minich <michal.minich@gmail.com> wrote:

> from high level point of view, there is difference in const(int)[2] and
> const (int [2]). One would expect that it is possible to rebind b. From
> low level/implementation point - there seems to be no difference because
> a and b are value types - there is not indirection.
>
> Possible resolutions:
>
> 1. a = [1,2]; should pass and it is a bug in current implementation
>
> 2. it is not a bug, then disallow writing const(int)[n] - only full const
> should be possible to use to prevent confusion.
>
> 3. it is not a bug, then update language definition to make static and
> dynamic arrays constnes modifiers act the same way.
>
> My first question is it a bug or not, then what would be good thing to
> do...

It is not a bug. As you say, static arrays are value types, and thus not
rebindable. This precludes your solution #1 and #3. As for solution #2,
it is not viable for genericity reasons - T[n] should work no matter what
T is (in this case const(int)). If you need rebindability, use a
(mutable)pointer to (const)static array.

-- 
Simen
Top | Discussion index | About this forum | D home