Phobos uses opSlice and opDollar without checking for it

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed May 25 14:43:19 PDT 2016


On Wednesday, May 25, 2016 15:05:33 qznc via Digitalmars-d wrote:
> Today I learned [0] that opDollar must be explicitly implemented
> and might not be available by some ranges. Likewise slicing. If
> you think it further, there are lots of functions in Phobos (I'm
> currently looking into std.algorithm.searching) which use more
> features than they check capabilities for. Ranges which satisfy
> isRandomAccessRange do not automatically satisfy hasSlicing.
>
> Can we provide opSlice and opDollar implicitly for random access
> ranges?
>
> Alternatively, we should comb through Phobos to fix the
> capability checks and write unittests with minimalistic ranges.
>
> I already filed issue 16073 [1], but then realized it does not go
> far enough.
>
> Also, std.range.primitives should have a predicate for opDollar
> similar to hasSlicing.
>
> [0] https://github.com/dlang/phobos/pull/4362#discussion_r64576690
> [1] https://issues.dlang.org/show_bug.cgi?id=16073

hasSlicing and isRandomAccessRange do require that opDollar work correctly
if they're there, but it would have been way too large a breaking change to
make them require opDollar, and it's kind of silly to make them implement
opDollar when the compiler could generate for them from length, which is why
https://issues.dlang.org/show_bug.cgi?id=7177 exists. Unfortunately, it's
never been implemented (IIRC due to issues related to AAs having length but
not being ranges; the implementation likely needs to check for the
range-based functions in addition to length). And as long as 7177 hasn't
been implemented, hasSlicind and isRandomAccessRange can't require $, and
until they require opDollar, Phobos should never use $ on ranges unless
they're guaranteed to be arrays or guaranteed to be ranges that implemented
opDollar, and that's really true of _any_ D code at this point, not just
Phobos. IMHO, we really should implement some form of 7177 so that we can
actually make it possible to use $ with ranges, but until that happens,
we're out of luck.

- Jonathan M Davis



More information about the Digitalmars-d mailing list