Forward ranges in Phobos v2
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.com
Wed Nov 3 15:40:41 UTC 2021
On 2021-11-02 20:38, Paul Backus wrote:
> 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;
> }
> }
OK, so the signature of next for all ranges is:
Option!(ElementType!R) next(Range)(ref Range);
Is that correct?
More information about the Digitalmars-d
mailing list