Consevutive calls to r.front

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 7 18:15:30 PDT 2015


On 6/8/2015 9:55 AM, Dennis Ritchie wrote:
> On Monday, 8 June 2015 at 00:42:12 UTC, Mike Parker wrote:
>> When implementing a custom range, is it correct to say that
>> consecutive calls to r.front with no intervening calls to popFront
>> should return the same value?
>
> Yes. For examle:
>
> import std.stdio, std.range;
>
> template foo(T) {
>      auto foo(R)(R range) {
>          while (!range.empty) {
>              writeln(range.front);
>              // .front --> the first element of the range
>              range.popFront;
>              // .popFront --> to extract the first element of the range
>          }
>      }
> }
>
> void main() {
>      foo!(int[])([1, 2, 3]);
> }

I know how to use a range :) What I'm asking about is a requirement on 
implementing front on a custom range. Is there a rule that says when I 
implement my own range, consecutive calls to front must return the same 
value until popFront is called?

Example:
Is this a valid implementation of front?
auto front() { return _member++; }

Or must it be this:
auto front() { return _member; }
void popFront() { ++_member; }

My current understanding is that the former is incorrect, but I'm 
looking for confirmation of that. I can't find it written down anywhere.




More information about the Digitalmars-d-learn mailing list