protocol for using InputRanges

Rainer Schuetze r.sagitario at gmx.de
Thu Mar 27 12:24:41 PDT 2014



On 27.03.2014 10:06, Walter Bright wrote:
> On 3/26/2014 11:53 PM, Rainer Schuetze wrote:
>>  This caching range example:
>>
>> ///////////////////////////////////
>> T getc(T)();
>>
>> struct irange(T)
>> {
>>    bool _cached;
>>    T _cache;
>>
>>    this(T[] arr) { _cached = false; }
>>    bool empty() {
>>      if(_cached) return false;
>>      _cache = getc!T();
>>      return (_cache < 0);
>
> What happens if empty is called twice in a row? Two characters get read!
> That isn't right.

Good catch. Somehow I pasted the wrong version, but posted the 
corrections a few minutes later.

>
>>    }
>>    T front() { empty(); return _cache; }
>
> What happens if empty returns true? EOF? I don't think that's intuitive.
> You could have front throw, but that prevents anyone from building
> nothrow ranges.

The caller is told to guarantee that empty must not return true. I just 
didn't wanted to repeat the stuff in empty. GDC removed it anyway...


More information about the Digitalmars-d mailing list