digitalmars.D.learn - range.put() to Empty Array Causes Error?
- Vijay Nayar (17/17) Jun 17 2018 This code breaks with the following error:
- Steven Schveighoffer (7/28) Jun 17 2018 range.put fills an existing array like a buffer, it does not append (as
- Seb (4/29) Jun 17 2018 Or simply ~= if you want to use built-in arrays (works with
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?
Jun 17 2018
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. BTW, use put(vals, 3) instead of vals.put(3), as you may bypass the features of put. -Steve
Jun 17 2018
On Sunday, 17 June 2018 at 12:23:55 UTC, Steven Schveighoffer wrote:On 6/17/18 7:07 AM, Vijay Nayar wrote:Or simply ~= if you want to use built-in arrays (works with Appender too FWIW).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.
Jun 17 2018