how to skip the next (n) item & continue from (n+1) with a range ? e.g. in File(fn).byLine();

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jun 22 21:22:10 UTC 2020


On Mon, Jun 22, 2020 at 08:51:49PM +0000, mw via Digitalmars-d-learn wrote:
[...]
> >    auto line = range.front;
> >    range.popFront;  // pop immediately
[...]

This is dangerous, because it assumes .front is not invalidated by
.popFront.  It will not work, for example, with byLine because .front
returns a buffer which is reused by .popFront (a so-called "transient
range").

In the realm of defensive programming, it is better to make less
assumptions (don't assume .front remains valid after .popFront) than add
implicit assumptions (range is non-transient) that may break in subtle
ways that are not immediately obvious.


T

-- 
Why ask rhetorical questions? -- JC


More information about the Digitalmars-d-learn mailing list