protocol for using InputRanges

Walter Bright newshound2 at digitalmars.com
Thu Mar 27 02:06:11 PDT 2014


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.

>    }
>    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.

>    void popFront() { _cached = false; }
> }



More information about the Digitalmars-d mailing list