Thread overview
Is [ 0, 1, 2 ] an immutable array?
Aug 11, 2009
Ali Cehreli
Aug 12, 2009
Ali Cehreli
August 11, 2009
Does the expression [ 0, 1, 2 ] form an immutable array? If so, is the assignment to a[0] undefined below? Is it trying to modify an immutable element?

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

The reason for my thinking that [ 0, 1, 2] is an array is because it has the .dup property and this works too:

    int[] a = [ 0, 1, 2 ].dup;

Thank you,
Ali

August 12, 2009
Ali Cehreli wrote:
> Does the expression [ 0, 1, 2 ] form an immutable array? If so, is the assignment to a[0] undefined below? Is it trying to modify an immutable element?
> 
>     int[] a = [ 0, 1, 2 ];
>     a[0] = 42;
> 
> The reason for my thinking that [ 0, 1, 2] is an array is because it has the .dup property and this works too:
> 
>     int[] a = [ 0, 1, 2 ].dup;
> 
> Thank you,
> Ali
> 


Nope, it's an ordinary, mutable array. :) To create an immutable array you can do like this:

  // This is an immutable array of ints:
  immutable int[] a = [ 0, 1, 2 ];

  // This is an array of immutable ints:
  immutable(int)[] a = [ 0, 1, 2 ];

The .dup property simply creates a copy of the array, which can be useful whether the array is immutable or not.

(Note that there will be some changes in array syntax/semantics from the next version of DMD2. In particular, arrays of type T[] will be unresizable. Resizable arrays will have a new type, denoted T[new].)

-Lars
August 12, 2009
On Tue, 11 Aug 2009 19:05:56 -0400, Ali Cehreli <acehreli@yahoo.com> wrote:

> Does the expression [ 0, 1, 2 ] form an immutable array? If so, is the assignment to a[0] undefined below? Is it trying to modify an immutable element?
>
>     int[] a = [ 0, 1, 2 ];
>     a[0] = 42;
>
> The reason for my thinking that [ 0, 1, 2] is an array is because it has the .dup property and this works too:
>
>     int[] a = [ 0, 1, 2 ].dup;
>

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 think there's even a bugzilla for that...

-Steve
August 12, 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

August 12, 2009
On Wed, 12 Aug 2009 12:41:20 -0400, Ali Cehreli <acehreli@yahoo.com> wrote:

> 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

Hold off.  Arrays and slices are about to change drastically (see thread on T[new] in digitalmars.D)

-Steve