opNext: Simplified ranges
Steven Schveighoffer
schveiguy at gmail.com
Sun Nov 6 17:05:22 UTC 2022
On 11/6/22 2:55 AM, Tomer at Weka wrote:
> On Sunday, 6 November 2022 at 01:48:04 UTC, Walter Bright wrote:
>> We didn't use the "next" protocol for ranges because one cannot test
>> for existence of the next element without consuming it.
>
> But that's exactly the issue - I have to produce the element in the
> range's ctor, which essentially requires a "non-popping popNext" to
> determine if it's empty and assign the front
The issue there is that you are making a copy, and the copy has already
consumed the front element. It would be no different with your API as
well -- if you break from your range type, the element is gone.
What we need is non-copyable input ranges. I have advocated for this for
a while. In essence, a forward range should be copyable, an input range
should not be, and we should remove `save`. Then when you foreach it,
you *can't* make a copy, so it doesn't let you discard the element you
are looking at.
The other option is -- don't use foreach. Use a while loop like you wrote.
-Steve
More information about the Digitalmars-d
mailing list