protocol for using InputRanges

Tobias Müller" <troplin at bluewin.ch> Tobias Müller" <troplin at bluewin.ch>
Fri Mar 28 16:14:55 PDT 2014


On Thursday, 27 March 2014 at 20:49:16 UTC, Walter Bright wrote:
> On 3/27/2014 12:21 PM, Rainer Schuetze wrote:
>> This loop is intuitive. Not being allowed to call empty or 
>> front multiple times
>> or not at all is unintuitive. They should not be named as if 
>> they are properties
>> then.
>
> I can concede that. But I can't concede being able to call 
> front without first calling empty, or calling popFront without 
> calling empty and front, or requiring 'pump priming' in the 
> constructor.

Disclaimer: I'm a C++ programmer just lurking here, I've never
actually used D.

I find it very counter-intuitive that 'empty' is required before
front or popFront.
Since 'pump priming' in the constructor isn't wanted either, i'd
suggest the following protocol:

while (popFront())
{
     front;
}

popFront is then required to return !empty.
'empty' as a separate property getter can stay but is not
required for the protocol.

This way, it's clear that the work to fetch the next element is
always done in popFront.

Generally I find dependecies between functions problematic that
require a specific call sequence. If they can be removed, they
should.

With my proposed solution, there's still one minor dependency,
namely that front is not valid before the first popFront. This
could be solved by again combining the two, as proposed by
someone else in this thread.

Tobi


More information about the Digitalmars-d mailing list