Thread overview
Question about mutable arrays
Nov 19, 2009
A Bothe
Nov 19, 2009
Ellery Newcomer
Nov 19, 2009
Phil Deets
Nov 19, 2009
Ali Cehreli
Nov 20, 2009
A Bothe
November 19, 2009
Hey guys,
I've found a problem that occurred since DMD 2.034:

When I've created a dynamic array like
string[] a;

and I want to assign something via the index of this array a[0]="Test";

DMD says the array isn't mutable...even if these are normal types like int or char.

Does anybody knows how to solve this problem?

Thank in advance
November 19, 2009
A Bothe wrote:
> Hey guys,
> I've found a problem that occurred since DMD 2.034:
> 
> When I've created a dynamic array like
> string[] a;
> 
> and I want to assign something via the index of this array a[0]="Test";
> 
> DMD says the array isn't mutable...even if these are normal types like int or char.
> 
> Does anybody knows how to solve this problem?
> 
> Thank in advance

a[0] = "Test".dup;

?
November 19, 2009
On Thu, 19 Nov 2009 09:16:16 -0500, A Bothe <info@alexanderbothe.com> wrote:

> Hey guys,
> I've found a problem that occurred since DMD 2.034:
>
> When I've created a dynamic array like
> string[] a;
>
> and I want to assign something via the index of this array
> a[0]="Test";
>
> DMD says the array isn't mutable...even if these are normal types like int or char.
>
> Does anybody knows how to solve this problem?
>
> Thank in advance

Does a still have length 0. You may be getting an array out of bounds error with a weird error message.

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
November 19, 2009
A Bothe Wrote:

> I've found a problem that occurred since DMD 2.034:

I am testing it with 2.036.

> When I've created a dynamic array like
> string[] a;

I like to see it a "slice." Even if we go with "dynamic array," I see it as an empty dynamic array.

> and I want to assign something via the index of this array a[0]="Test";

I would expect it to be illegal to access a[0], because a is empty.

> DMD says the array isn't mutable...

2.036 says "Error: null dereference in function _Dmain" and it matches my expectation above.

Ali

November 20, 2009
These lines were just a basic idea...of course I have to fill the array...

string[] a=new string[2];

a[0]="A";
a[1]="B";

my idea was to create a list class to store something in it...

class List(T)
{
T[] arr;

// ctors and such

void opIndexAssign(T e,int i)
{
     arr[i]=e; // And here it throws the exception that arr is not mutable...
}
}
November 23, 2009
On Fri, 20 Nov 2009 09:42:55 -0500, A Bothe <info@alexanderbothe.com> wrote:

> These lines were just a basic idea...of course I have to fill the array...
>
> string[] a=new string[2];
>
> a[0]="A";
> a[1]="B";
>
> my idea was to create a list class to store something in it...
>
> class List(T)
> {
> T[] arr;
>
> // ctors and such
>
> void opIndexAssign(T e,int i)
> {
>      arr[i]=e; // And here it throws the exception that arr is not mutable...
> }
> }

First, it looks like you are assuming you can just assign an element of an array without ensuring the array is large enough to hold it.  This works in some dynamic languages but not D.

Second, if you have done that properly, this should work if T is string, because although string is not fully "mutable", it is rebindable.

Can you post a full code example, and the exact error message you get?  It's hard to guess what problem you are running into.  You don't need to post full code, just enough to make the error occur.

-Steve