Array literals MUST be immutable.

Don nospam at nospam.com
Thu Feb 18 00:10:47 PST 2010


Michel Fortin wrote:
> On 2010-02-17 20:07:20 -0500, Michael Rynn <michaelrynn at optusnet.com.au> 
> said:
> 
>> Is there anything that is really awkward in D2 working with immutable
>> string type, that cannot be done without resorting to mutable character
>> literals?   If not, then the principle can be applied to all "immutable
>> source".
> 
> Well, array literals are not always constants. Take this for instance:
> 
>     int[] array = [x, y, z];
> 
> This array literal does not have an "immutable source" as you call it.

This is the issue. The syntax sugar you get from not requiring an 
"immutable source" comes at a very high price.

> Should it really require a ".dup" to get a mutable array? How efficient 
> would it be?

What it does at the moment is insert a hidden .dup at all times. And the 
performance is terrible. Really, really terrible. Roughly 100 times 
worse than you expect.
At the moment, [1, 2, 3] is basically transformed into something like 
makeArray(1,2,3);




More information about the Digitalmars-d mailing list