Dynamic array ot not

Ali Çehreli acehreli at yahoo.com
Sun Jan 16 04:58:21 UTC 2022


On 1/15/22 20:09, forkit wrote:
 > so at this link: https://dlang.org/spec/arrays.html
 >
 > it indicates an array of type[] is of type 'Dynamic array'.

I have a problem with calling type[] a dynamic array because it is a 
slice, which may be providing access to the elements of a dynamic array.

One complexity in D's slices is that they will start providing access to 
a dynamic array upon appending or concatenation.

Here is the proof:

void main() {
   int[2] data;
   int[] slice = data[];
}

'slice' has *nothing* to do with any dynamic array. Period!

It will provide access to one with the following operation:

   slice ~= 42;

Again, 'slice' is not a dynamic array; it provides access to the 
elements of one. (That dynamic array is owned by the D runtime (more 
precisely, the GC).)

Despite that reality, people want dynamic arrays in the language and 
call slices dynamic arrays. Ok, I feel better now. :)

 > with that in mind, I ask, is this below a 'Dynamic array'.

Nothing different from what I wrote above.

 > If not, why not?
 >
 >
 > int[][] mArr2 = array(iota(1, 9).chunks(2).map!array.array);

mArr2 is a slice of slices. We can see from the initialization that 
there are many dynamic arrays behind the scenes.

The outermost array() call is unnecessary because array() of a slice 
will produce another slice with the same type (perhaps e.g. a 'const' 
might be dropped) and elements. This is the same:

   int[][] mArr2 = iota(1, 9).chunks(2).map!array.array ;

Ali



More information about the Digitalmars-d-learn mailing list