std.range's put on arrays needs to change

Paul Backus snarwin at gmail.com
Sun Jun 26 04:21:46 UTC 2022


On Saturday, 25 June 2022 at 23:44:59 UTC, WebFreak001 wrote:
> I strongly feel that this behavior is very inconsistent and 
> unexpected for most users and should be changed. I think we 
> should deprecate simple arrays/slices on `put` and rename the 
> method for those.
>
> I propose put with slices should be renamed to `putInto`.

I agree that `put` on arrays does not do what you usually want it 
to do, but I do not agree that it is inconsistent. It is exactly 
the same behavior you would get if you used the input range 
interface and wrote `arr.front = 2; arr.popFront;`.

The bigger problem is that unlike an input range's `empty` 
method, there is no generic way to tell if an output range is 
full before you try to `put` something into it. And, since `put` 
fails with an `Error` rather than an `Exception`, you cannot 
handle failure after the fact either--the only thing you can do 
is crash.

IMO `put` should be changed in `std.v2` to either throw an 
exception or return `false` when attempting to insert an element 
into a full output range.


More information about the Digitalmars-d mailing list