Using .length returns incorrect number of elements

Chris M. chrismohrfeld at comcast.net
Sun Aug 19 15:49:18 UTC 2018


On Sunday, 19 August 2018 at 15:44:07 UTC, QueenSvetlana wrote:
> When using the .length property of a dynamic array why does it 
> return the incorrect number of elements after I use the 
> appender?
>
> import std.stdio;
> import std.array : appender;
>
> void main()
> {
>     //declaring a dynamic array
>     int [] arrayofNumbers;
>     //append an element using the ~= syntax
>     arrayofNumbers ~= 1;
>     arrayofNumbers ~= 2;
>     //print the array
>     writeln(arrayofNumbers);
>
>     //Using appender
>     auto appendNumber = appender(arrayofNumbers);
>     appendNumber.put(10);
>     writeln(appendNumber.data);
>
>     writeln(arrayofNumbers.length);
>
> }
>
> Output:
>
> [1, 2]
> [1, 2, 10]
> 2  --- > Should be 3

auto appendNumber = appender(arrayofNumbers);

This returns a separate object. You probably meant to put this 
for the last line

writeln(appendNumber.length);


More information about the Digitalmars-d-learn mailing list