Zero length arrays in D

Eduard Staniloiu edi33416 at
Mon Feb 18 13:34:37 UTC 2019


According to the spec[0], D supports zero length arrays [1].

I have given this a shot at

Attempting to use the zero-length array results in a compiler 

`a.contents[2]` -> Error: array index 2 is out of bounds 
(*a).contents[0 .. 0]

The way I've used around this error is "safely" break/trick the 
type system

     int *p = a.contents.ptr;
     int[] p_cont = p[0 .. n];
     p_cont[2] = 10; // fine

Is this the intended way of working with zero length arrays?


[0] -, best 
practices pt 2
[1] -

More information about the Digitalmars-d-learn mailing list