Array literals MUST be immutable.

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


On 2010-02-17 18:38:55 -0500, Steven Schveighoffer <schveiguy at 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 at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list