Forward ranges in Phobos v2

Andrei Alexandrescu SeeWebsiteForEmail at erdani.com
Wed Nov 3 17:41:10 UTC 2021


On 2021-11-03 12:18, Paul Backus wrote:
> On Wednesday, 3 November 2021 at 15:40:41 UTC, Andrei Alexandrescu wrote:
>> On 2021-11-02 20:38, Paul Backus wrote:
>>>
>>> auto next(R)(ref R r)
>>>      if (isForwardRangeV2!R && isMutable!R)
>>> {
>>>      alias E = ElementType!R;
>>>      if (r.empty)
>>>          return none!E();
>>>      else
>>>      {
>>>          auto result = some(r.head);
>>>          r = r.tail;
>>>          return result;
>>>      }
>>> }
>>
>> OK, so the signature of next for all ranges is:
>>
>> Option!(ElementType!R) next(Range)(ref Range);
>>
>> Is that correct?
> 
> More precisely, to use the Phobos convention: `is(ReturnType!((Range r) 
> => r.next) == Option!(ElementType!R))`.
> 
> So, `next` could be a function, a @property, or a member variable, and 
> it does not necessarily require an lvalue to call (just like `front` 
> today).

We've considered this way back when. I'm talking like 2006. It was like 
this:

T next(Range)(ref Range r, ref bool done);

The main problem is that iterating such a forward range would entail a 
copy of each element of the range. This is not scalable in general.

This is a showstopper.


More information about the Digitalmars-d mailing list