Consevutive calls to r.front

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 7 18:29:17 PDT 2015


On Monday, 8 June 2015 at 01:15:30 UTC, Mike Parker wrote:
> 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.

Here is how it is implemented in the book of Andrew:

@property bool empty(T)(T[] a) { return a.length == 0; }
@property ref T front(T)(T[] a) { return a[0]; }
void popFront(T)(ref T[] a) { a = a[1 .. $]; }


More information about the Digitalmars-d-learn mailing list