Can't recreate a range?

Steven Schveighoffer schveiguy at
Thu Apr 30 19:07:49 UTC 2020

On 4/30/20 2:05 PM, Paul Backus wrote:
> On Thursday, 30 April 2020 at 16:21:05 UTC, Steven Schveighoffer wrote:
>> I would say part of the issue is that you are doing all your work in 
>> front and not popFront.
> [...]
>> I'd say:
>> 1. move your work to the popFront function (you then need to call 
>> popFront once before returning the range in your factory method).
>> 2. change empty to check if the buffer is empty instead of the input.
> Doing work in popFront instead of front is usually an anti-pattern, 
> since it forces eager evaluation of the next element even when that 
> element is never used. You should only do this if there's no reasonable 
> way to avoid it.

In the general scheme of things, this only makes sense for the first 
front access or empty access. From then on, popFront should do the work. 
In most cases, you can't tell a range is empty unless you've done the 
work to determine the next element. After you call popFront, you need to 
call empty before calling front, or else you may be using an invalid 
range. So all the work is going to be done anyway.

A good example, byLine. How do you know that there is no more data until 
you attempt to read the next line from the stream? At which point, you 
have to do all the work.


P.S. I see H.S. Teoh said much of the same thing, but I had already 
typed this out before reading it ;)

More information about the Digitalmars-d-learn mailing list