attribute length missing in std.array: Appender

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 28 00:44:54 PST 2014


Ali Çehreli:

>> auto app = appender!(ubyte[])();
>> app.put(cast(ubyte)40);
>> app.put(cast(ubyte)5);
>> app.put(cast(ubyte)234);
>> // ... add 5 times 0
>
> A fancy way: :)
>
> import std.range;
>
> // ...
>
>     app.put(repeat(cast(ubyte)0).take(5));

Now we have a better syntax for implicit casts:

void main() {
     import std.array, std.range;
     Appender!(ubyte[]) app;
     app.put(ubyte(0).repeat.take(5));
}


Single line, but not lazy:

void main() {
     import std.array, std.range;
     Appender!(ubyte[]) app = ubyte(0).repeat.take(5).array;
}


But I have a question too. What's the best way to append several 
lazy items to a dynamic array? This doesn't work:

void main() {
     import std.array, std.range;
     int[] arr;
     arr.put(only(1, 2, 3));
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list