Forward ranges in Phobos v2

Paul Backus snarwin at gmail.com
Wed Nov 3 00:38:37 UTC 2021


On Wednesday, 3 November 2021 at 00:24:11 UTC, H. S. Teoh wrote:
> On Wed, Nov 03, 2021 at 12:18:59AM +0000, Paul Backus via 
> Digitalmars-d wrote:
>>
>> Having input ranges implement `next` and forward ranges 
>> implement `head` and `tail` would also make them easy to 
>> distinguish.
>
> That would work too, but makes the input range API no longer a 
> subset of the forward range API.  This would lead to code 
> duplication in algorithms that only require an input range but 
> could work equally well with a forward range.

Not necessarily. It's possible to implement `next` as a UFCS 
function for mutable forward ranges using the `head`/`tail` API:

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;
     }
}


More information about the Digitalmars-d mailing list