range.put() to Empty Array Causes Error?

Seb seb at wilzba.ch
Sun Jun 17 13:59:25 UTC 2018


On Sunday, 17 June 2018 at 12:23:55 UTC, Steven Schveighoffer 
wrote:
> On 6/17/18 7:07 AM, Vijay Nayar wrote:
>> This code breaks with the following error:
>> void main()
>> {
>>      import std.range;
>>        int[] vals = [];
>>      vals.put(3);
>> }
>> /src/phobos/std/range/primitives.d(2328): Attempting to fetch 
>> the front of an empty array of int
>> 
>> The following code has no error:
>> void main()
>> {
>>      import std.range;
>>        int[] vals = [1];
>>      vals.put(3);
>> }
>> 
>> Why is range.put() not allowed for empty arrays?
>> 
>> 
>
> range.put fills an existing array like a buffer, it does not 
> append (as I'm guessing you are expecting). Use 
> std.array.Appender to get append behavior.

Or simply ~= if you want to use built-in arrays (works with 
Appender too FWIW).



More information about the Digitalmars-d-learn mailing list