grauzone wrote:
Denis Koroskin wrote:
On Tue, 28 Apr 2009 22:13:50 +0400, Tomas Lindquist Olsen
<[email protected]> wrote:
On Tue, Apr 28, 2009 at 6:07 PM, MLT <[email protected]> wrote:
2. char[] vs. int[]
I think it is strange that
char[] x = "1234" ;
x[0] = '4' ;
Produces a run time error, but
int[] x = [1,2,3,4] ;
x[0] = 4 ;
Doesn't. I think that they both should, or both shouldn't - to be
consistent (and it would be better if they both didn't). Best would
be again, to allow the programmer to specify where the array (or
other stuff) should be stored.
It pretty much boils down to this (mostly said in other replies)
* string literals are special, they are allocated in a static data
segment, readonly if the platform allows it.
* arrayliterals as non-static expressions are always heap allocated.
even when there's absolute no need for it... (see
http://d.puremagic.com/issues/show_bug.cgi?id=2356 )
-Tomas
Someone ought to post a feature request to make arrayliterals
immutable by default (D2 only, I don't think this change will go to D1
anyway) and don't heap-allocate them every time they're used:
Definitely. They should go into the initialized data segment.
int[] x1 = [1,2,3,4]; // error: can't assign immutable(int)[] to int[]
Why not auto-allocate? Writing that .dup all the time is going to be
annoying. Just implicitly convert it from immutable to mutable by copying.
I think it's pretty unlikely that you'd want to do that. It's far more
likely that it's a bug, and you meant it to be an immutable(int)[].
Except in the case where the array is empty.
(Likewise, it'd be nice if char [] s=""; were legal in D2. Since a
mutable "" string has nothing that can be changed anyway, "" can be
treated in the same way that 'null' is).
int[] x2 = [1,2,3,4].dup; // okay, allocates
int[4] x3 = [1,2,3,4]; // okay, doesn't allocate
foreach (int i; [1,2,3,4]) { // okay, doesn't allocate
// ...
}