protocol for using InputRanges

Dicebot public at dicebot.lv
Mon Mar 24 06:20:54 PDT 2014


On Sunday, 23 March 2014 at 00:50:34 UTC, Walter Bright wrote:
> It's become clear to me that we've underspecified what an 
> InputRange is. The normal way to use it is:
>
>     while (!r.empty) {
>         auto e = r.front;
>         ... do something with e ...
>         r.popFront();
>     }
>
> no argument there. But there are two issues:
>
> 1. If you know the range is not empty, is it allowed to call 
> r.front without calling r.empty first?
>
> If this is true, extra logic will need to be added to r.front 
> in many cases.
>
> 2. Can r.front be called n times in a row? I.e. is calling 
> front() destructive?
>
> If true, this means that r.front will have to cache a copy in 
> many cases.

I think there is one design mistake with current InputRange rules 
that makes usage so inconsistent. We have `empty` but don't have 
any distinct `not yet started` state. If calling `popFront` at 
least once was required before accessing `front`, I can't imagine 
the case where having any non-trivial code in `front` would have 
been necessary.


More information about the Digitalmars-d mailing list