getNext
Shin Fujishiro
rsinfu at gmail.com
Wed Jul 14 10:33:57 PDT 2010
Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> 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.
>
> getNext is easy to define for e.g. arrays and files. How does it sound?
> Does it bring significant simplification?
I gave it a try, and it fairly simplified range implementation.
http://gist.github.com/474562
I think it's good.
(1) But how does it represent an infinite range?
(2) Should user code use getNext for input ranges (like put for output
ranges)? For example:
void doSomething(R)(R input)
{
// read first character if any
dchar* p = getNext!dchar(input);
if (p == null)
return;
dchar a = *p;
// read subsequent characters...
dchar b = *enforce( getNext!dchar(input) );
...
}
Shin
More information about the Digitalmars-d
mailing list