On 07/12/2010 10:48 PM, Andrei Alexandrescu wrote:
I think I figured out a comfortable and all-encompassing means to define
a simplified interface for an input range.

Currently input ranges need to define empty, front, and popFront. That
works but it's a bit heavy for simple input ranges. We've been
discussing simplified interfaces in this group but couldn't find one
that satisfied all use cases.

Consider this:

T* getNext(R, T)(ref R range, ref T item);

Semantics: if the range wants to expose addresses of its elements, it
returns a pointer to the current element and also advances to the next
element. Otherwise (i.e. the range does not have or does not want to
expose addresses of its elements), the range fills "item" with the
current value, again moves on to the next value, and returns &item.

In all cases, when there are no more elements in the range, getNext
returns null.

let's see here

Range AhmAStream;

ubyte[] buf = new ubyte[4];
ubyte[]* ruf = AhmAStream.getNext(buf);
assert(ruf && ruf.length == 4);
int i = * (cast(int*) ruf.ptr)
buf = new ubyte[i];
ruf = AhmAStream.getNext(buf);
assert(ruf && ruf.length == i);

Something like this could work within this interface, couldn't it?

Think you might be on to something


getNext is easy to define for e.g. arrays and files. How does it sound?
Does it bring significant simplification?


Andrei

Reply via email to