Dynamic array ot not

forkit forkit at gmail.com
Mon Jan 17 02:42:40 UTC 2022


On Monday, 17 January 2022 at 00:54:19 UTC, forkit wrote:
> ..


module test;

import std;

@safe void main()
{
     // mArr1 is a dynamic array allocated on the gc heap.
     int[][] mArr1 = [[1, 2], [3, 4], [5, 6], [7, 8]];
     static assert(is(typeof(mArr1.array())));
     alias R1 = typeof(mArr1);
     static assert(isRandomAccessRange!R1 &&
                   isForwardRange!R1 &&
                   isInputRange!R1 &&
                   isBidirectionalRange!R1 &&
                   hasAssignableElements!R1 &&
                   !isInfinite!R1 &&
                   hasSlicing!R1);

     // mArr2 is a dynamic array allocated on the gc heap
     // although compiling with '-profile=gc' incorrectly suggests 
otherwise.
     int[][] mArr2 = iota(1, 9).chunks(2).map!array.array;
     static assert(is(typeof(mArr2.array())));
     alias R2 = typeof(mArr2);
     static assert(isRandomAccessRange!R2 &&
                   isForwardRange!R2 &&
                   isInputRange!R2 &&
                   isBidirectionalRange!R2 &&
                   hasAssignableElements!R2 &&
                   !isInfinite!R2 &&
                   hasSlicing!R2);

     static assert(is(typeof(mArr1) == typeof(mArr2)));

}

/+

if add @nogc to above code:
(8): Error: array literal in `@nogc` function `D main` may cause 
a GC allocation
(22): Error: `@nogc` function `D main` cannot call non- at nogc 
function `std.array.array!(MapResult!(array, 
Chunks!(Result))).array`

+/


More information about the Digitalmars-d-learn mailing list