Phobos uses opSlice and opDollar without checking for it

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed May 25 23:07:36 PDT 2016


On Wednesday, May 25, 2016 22:08:31 Era Scarecrow via Digitalmars-d wrote:
> On Wednesday, 25 May 2016 at 21:43:19 UTC, Jonathan M Davis wrote:
> > 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, hasSlicing 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.
>
>   Hmmm if an algorithm can make use of opDollar, we should be able
> to test for it. Maybe extend the tests that include it say
> hasFullSlicing? Unless something explicitly needs opDollar then
> it probably shouldn't be included. Someone's already mentioned
> infinite ranges/arrays, and AA not using $ would cause major
> breakages.

Infinite ranges will need to implement opDollar if they want to work with
opDollar, but infinite ranges typically aren't sliceable anyway. However,
hasSlicing does test that if they use opDollar correctly if they support it.
I think that it's overkill to add something like hasFullSlicing to have a
versions of hasSlicing that requires opDollar. It's downright silly really
that we have slicing without it, but I really don't think that it's worth
adding yet another range trait to check for it. At this point, generic code
should just never use opDollar unless it specifically tests for it, and
usually, there's really no reason to do that rather than just using length
at this point, much as it's far more aesthetically pleasing to use $. But
ranges really should support $ just like arrays do, so the current state of
things is definitely annoying.

>   Are there any major patterns that we can't implement without
> opDollar? Or are there any that would perform better with it
> present?

The only reason you would _need_ opDollar is for slicing infinite ranges to
the end, and most infinite ranges aren't sliceable. Otherwise, using
opDollar and using explicit length should be identical. And if they aren't
there's either something funny about how slicing was implemented and/or the
optimizer isn't doing its job.

- Jonathan M Davis



More information about the Digitalmars-d mailing list