More pathological range subtleties

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Feb 12 12:43:54 PST 2013


On Tue, Feb 12, 2013 at 09:29:57PM +0100, Benjamin Thaut wrote:
> Am 12.02.2013 21:22, schrieb H. S. Teoh:
> >
> >Note that .front returns a slice of _src. But this slice is constructed
> >*each time* you call .front. Which means the slice that fun called
> >.popFront on has no effect on the range at all. So fun will return the
> >original range in this case -- this is case (3).
> >
> 
> But fun never calls front, fun calls popFront and popFront does
> indeed change the internal slice?
[...]

There are two levels of ranges going on here. The outer range's .front
is what gets assigned to ref e, and when you call e.popFront, it indeed
changes the slice returned by the outer range's .front. However, the
problem is that the outer range's .front *always* creates a new slice of
the underlying array, and it never keeps track of older slices. So no
matter what you do with the slice returned by .front, it doesn't change
the outer range at all, and .front will continue to return a slice over
the same elements -- the e.popFront has no effect on it.


T

-- 
He who sacrifices functionality for ease of use, loses both and deserves neither. -- Slashdotter


More information about the Digitalmars-d mailing list