www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Array literals MUST be immutable.

reply Steven Schveighoffer <schveiguy yahoo.com> writes:
Michel Fortin Wrote:

 On 2010-02-17 04:17:09 -0500, Walter Bright <newshound1 digitalmars.com> said:
 
 Don wrote:
 This is for me the last remaining D2 issue.

That would make it difficult to do things like: int*[] foo(int *p) { return [p, p + 1]; } as all the elements of the literal would also have to be immutable. I think you've made a good case, but there is also this issue.

One thing I like to do with array literals is use them for appending several elements at once: array ~= [1, a+1, b+1]; This way I avoid multiple array appends so it should be faster, but if that temporary array literal gets allocated on the heap then it's rather counterproductive for me to use array literals for this. Could cases like this, where the array never escape, be allocated on the stack?

Doesn't this do exactly that now? array ~= 1 ~ (a+1) ~ (b+1); -Steve
Feb 17 2010
parent Michel Fortin <michel.fortin michelf.com> writes:
On 2010-02-17 18:38:55 -0500, Steven Schveighoffer <schveiguy yahoo.com> said:

 One thing I like to do with array literals is use them for appending
 several elements at once:
 
 	array ~= [1, a+1, b+1];
 
 This way I avoid multiple array appends so it should be faster, but if
 that temporary array literal gets allocated on the heap then it's
 rather counterproductive for me to use array literals for this.
 
 Could cases like this, where the array never escape, be allocated on the stack?

Doesn't this do exactly that now? array ~= 1 ~ (a+1) ~ (b+1);

It doesn't compile: Error: Can only concatenate arrays, not (int ~ int). This works fine: array ~= [1] ~ (a+1) ~ (b+1); but now we have added back an array literal. :-) -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Feb 17 2010