In general, who should do more work: popFront or front?

Mike Parker aldacron at gmail.com
Tue Jun 15 06:13:17 UTC 2021


On Tuesday, 15 June 2021 at 04:24:09 UTC, surlymoor wrote:
> 
> At the moment, I feel that as long as the stashed front element 
> isn't too "big" (For some definition of big, I guess.), that 
> built-in caching should be fine. But is this acceptable? What's 
> the best practice for determining which range member should 
> perform what work? (Other than iterating, of course.)

The "general rule" is that `front` should return the same result 
on multiple calls until after the next call to `popFront`. I 
don't know of any requirement that such an operation be O(1), but 
the expectation is certainly there. The implication then is that 
any necessary work should be carried out in `popFront` to advance 
the range (and additionally in a constructor if things need to be 
teed up first) and that `front` should just return the current 
element.



More information about the Digitalmars-d-learn mailing list