Dynamic array of strings and appending a zero length array

FeepingCreature feepingcreature at gmail.com
Sat Jul 8 17:34:43 UTC 2023


On Saturday, 8 July 2023 at 17:15:26 UTC, Cecil Ward wrote:
> I have a dynamic array of dstrings and I’m spending dstrings to 
> it. At one point I need to append a zero-length string just to 
> increase the length of the array by one but I can’t have a slot 
> containing garbage. I thought about ++arr.length - would that 
> work, while giving me valid contents to the final slot ?
>
> What I first did was
>    arr ~= [];
>
> This gave no errors but it doesn’t increase the array length, 
> so it seems. Is that a bug ? Or is it supposed to do that?
>

You can append an element to an array. You can also append an 
array to an array. Because [] can be an array of any type, the 
compiler guesses that it's an empty `string[]` array and appends 
it to no effect.


More information about the Digitalmars-d-learn mailing list