Array literals REALLY should be immutable

Steven Schveighoffer schveiguy at yahoo.com
Thu Nov 12 12:13:52 PST 2009


On Thu, 12 Nov 2009 14:46:29 -0500, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> Don wrote:
>> Especially when it's creating an inconsistency with string literals.
>
>
> The inconsistency bothers me, too, but then there's the case:
>
>     int x;
>     ...
>     [1, 2, x]
>
> That can't be made immutable. Shouldn't it work? There's no analog for  
> that for string literals, so the inconsistency isn't quite complete.

I thought so too, but I think Don is right.  A library function can solve  
that problem:

auto arr = array(1,2,x);

BTW, there is legitimate inconsistency here:

int[] x = [1,2,3]; // compiles and does what you expect
char[] str = "abc"; // should allocate a mutable string on the heap,  
should it not?

-Steve



More information about the Digitalmars-d mailing list