Is this a desing rationale? (static array object member)

Janice Caron caron800 at googlemail.com
Sat Sep 29 22:24:06 PDT 2007


On 9/30/07, janderson <askme at me.com> wrote:
> sections like [1,1,1,1,1] would be marked as const somehow in the GC (or
> somewhere).  If a modification occurs it would make a call to dup the
> first time.

This is all solved in D2.0, so really it's a non-issue.

int[] x = [1,1,1,1,1]; /* won't compile */

const(int)[] x = [1,1,1,1,1]; /* OK, but now you can't accidently
modify the array */

int[] x = [1,1,1,1,1].dup; /* OK */

The fact that the first version won't compile is precisely why const
correctness is a good thing. Without const, there's nothing to stop
you typing that line of code into a program and hitting undefined
behavior, and not even realising it. const saves the day.



More information about the Digitalmars-d mailing list