Accessors, byLine, input ranges

Steven Schveighoffer schveiguy at yahoo.com
Fri Jan 29 08:54:41 PST 2010


On Fri, 29 Jan 2010 11:33:17 -0500, Michel Fortin  
<michel.fortin at michelf.com> wrote:

> On 2010-01-29 11:18:46 -0500, Andrei Alexandrescu  
> <SeeWebsiteForEmail at erdani.org> said:
>
>> It should work for stream ranges if front() checks the "filled" flag  
>> and eats the next line if false, and popFront clears the "filled" flag.
>
> So now you want the front to fetch from stdin on the first call? It's  
> the same problem as 'byLine' eating the first line when you create it:  
> neither one or the other should affect the stream.

No, you're reaching here :)  What Andrei is doing is acknowledging that  
front has already performed the job of popFront.  Because of the nature of  
streams, you cannot get the data from the stream, and leave it on the  
stream at the same time.  It's just not feasible.

What the solution Andrei came up with does is to make stream ranges behave  
as close as possible to forward ranges.  That small inconsistency will not  
hurt you because most of the time you are not calling front for an element  
you don't intend to use.  And even within that paradigm, you are even less  
likely to use the stream in another capacity.

In other words, as the last usage of a range in an algorithm function,  
this:

r.front;
r.popFront();

is way more likely than:

r.popFront()
r.front;

-Steve



More information about the Digitalmars-d mailing list