Array literals MUST be immutable.

Michel Fortin michel.fortin at michelf.com
Wed Feb 17 05:44:28 PST 2010


On 2010-02-17 04:17:09 -0500, Walter Bright <newshound1 at 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?

Just like local variables get allocated on the heap when they escape 
through a closure, array literals could be allocated on the heap when 
they escape through a return or some other mean. They could also be 
part of the program's static data when the literal expression is 
constant and assigned to an immutable(T)[].

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list