www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about mutable arrays

reply A Bothe <info alexanderbothe.com> writes:
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
Nov 19 2009
next sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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; ?
Nov 19 2009
prev sibling next sibling parent "Phil Deets" <pjdeets2 gmail.com> writes:
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/
Nov 19 2009
prev sibling parent reply Ali Cehreli <acehreli yahoo.com> writes:
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
Nov 19 2009
parent reply A Bothe <info alexanderbothe.com> writes:
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...
}
}
Nov 20 2009
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Nov 23 2009