protocol for using InputRanges
Steven Schveighoffer
schveiguy at yahoo.com
Thu Mar 27 14:31:44 PDT 2014
On Thu, 27 Mar 2014 17:24:11 -0400, Walter Bright
<newshound2 at digitalmars.com> wrote:
> On 3/27/2014 2:13 PM, Steven Schveighoffer wrote:
>> Note that a stream makes a terrible range, simply because of what you
>> say --
>> reading is destructive, and determining emptiness is dependent on
>> reading. You
>> need a buffered stream to make a good range, and then the logic becomes
>> much
>> more straightforward.
>
> The range becomes the one element buffer in this case. It is completely
> workable.
>
A 1 byte buffered stream? If it's performance you are looking for, you
will not find it there.
> The range protocol is designed to work with streams. It's a giant fail
> if they do not, or if you want to create a separate, non-range universe
> to deal with streams.
Ranges work well on top of buffered streams, but not AS streams.
>> Even a single byte buffer is not enough, you need a bool to indicate
>> the stream
>> is done.
>
> Right. But empty for a stream still has to read. Just follow the
> protocol, and the range will work, even with streams.
>
A stream requires one primitive -- read. In one function call, you get the
data you want, you can tell if it's empty, and the stream object does not
need to concern itself with projecting a cached element. Adding range
primitives on top of a stream does not make sense.
-Steve
More information about the Digitalmars-d
mailing list