protocol for using InputRanges

Chris wendlec at tcd.ie
Thu Mar 27 03:21:39 PDT 2014


On Sunday, 23 March 2014 at 01:07:27 UTC, Rikki Cattermole wrote:
> 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.
>
> It would be nice to have ranges full stop described. And how to 
> use/make them in D. I have yet to learn them because of no 
> official documentation on them.
> On this topic we also need to work on common patterns and 
> creating documentation on e.g. the site or wiki for it. Saw 
> that we do have a bit in the wiki under tutorials. Maybe if I 
> get some time I'll work on that.

I agree. I've been using ranges for a while now and have tried
different implementations based on advice given on this forum and
depending on each case. After reading this thread I looked at
some of my ranges and I have to say I still have no clue as to
what should and should _not_ be done (regardless of whether you
_can_ do it). For a while I thought that it's my lack of
understanding, but this thread shows that everyone has a
different view of ranges. Guidelines with use cases would be
great. I remember I mentioned this in another thread already. The
thing is that experimenting without proper guidelines leaves your
code in an inconsistent state where you have two or more ranges
doing technically the same thing but each with a different logic.


More information about the Digitalmars-d mailing list