More range woes: std.array.save is invalid

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Dec 19 23:44:12 PST 2012


On Thu, Dec 20, 2012 at 02:36:18AM -0500, Andrei Alexandrescu wrote:
> On 12/20/12 2:03 AM, H. S. Teoh wrote:
> >http://d.puremagic.com/issues/show_bug.cgi?id=8061
> >
> >After being sidetracked by the stack-allocated buffer fiasco, I
> >finally found the root problem in the above bug.
> >
> >The problem is std.array.save:
> >
> >@property T[] save(T)(T[] a) @safe pure nothrow
> >{
> >     return a;
> >}
> >
> >This implementation is only correct for certain values of T. It is
> >wrong when T is a forward range, for example, because if you save a
> >T[] and iterate over its elements, it will consume the T's in the
> >array, so that the original range has elements that are now consumed.
> >
> >The correct implementation when T is a forward range is to return a
> >copy of the array where the elements are obtained by calling T.save.
> 
> I think you're overthinking this. Save saves the position in the
> current range without any promise about its contents.
[...]

Yes, which means many current algorithms that take a range of ranges and
returns a wrapper range are wrong, because they assume that the wrapper
range can be a forward range when both the container and the subranges
are forward ranges, but this is not a sufficient condition in the
general case.


T

-- 
Doubt is a self-fulfilling prophecy.


More information about the Digitalmars-d mailing list