Is [ 0, 1, 2 ] an immutable array?

Ali Cehreli acehreli at yahoo.com
Wed Aug 12 09:41:20 PDT 2009


Steven Schveighoffer Wrote:

> >     int[] a = [ 0, 1, 2 ];
> >     a[0] = 42;

> No, it's a mutable array.  It's one of the quirks of D2 that bugs me.  A  
> string literal is an immutable array but a normal array literal actually  
> allocates new space on the heap for the array every time you use it.  So  
> if you assign the same literal to 2 different variables, they are 2  
> separate copies of the array.
> 
> I think the behavior should be identical to strings.

I agree. I thought that D was a good first language to teach, so I've started to write a tutorial; but I am having big difficultly extracting the semantics of arrays and slices.

I still can't understand how to explain dynamic arrays and slices even to myself yet. :D

Could anyone point me to documentation that would clarify these issues for me, for a person who thinks he knows C and C++ arrays and vectors pretty well? :p

Andrei, can we review your chapter on arrays please? ;)

Thank you,
Ali



More information about the Digitalmars-d-learn mailing list