Various algorithms in std.algorithm.searching use opDollar, although given ranges might not implement it. For example:
https://github.com/dlang/phobos/blob/master/std/algorithm/searching.d#L1781
These should be converted into `x.length` instead and unittests should be inserted to check that range algorithms work without opDollar.
Comment #1 by jack — 2016-05-25T14:42:45Z
I'd imagine this is an issue throughout Phobos. All functions that use isRandomAccessRange need to be checked as well.
I think we should require opDollar to be implemented instead.
Comment #8 by jack — 2016-12-24T14:49:36Z
(In reply to Andrei Alexandrescu from comment #7)
> I think we should require opDollar to be implemented instead.
That would be a breaking change, as suddenly hasLength would fail for lots of user types.
Comment #9 by andrei — 2016-12-24T14:52:20Z
We can leave hasLength as is for the time but at least new algorithms should just use $. All of these useless options left to sloppy range implementations don't bring value.
Comment #10 by greeenify — 2016-12-24T15:08:37Z
> All of these useless options left to sloppy range implementations don't bring value.
This has probably been proposed before, but what about letting the compiler insert an opDollar alias if length is defined? This should greatly help to avoid sloppy implementation differences and one can still opt to use the opDollar overloads for superior performance.
Comment #11 by github-bugzilla — 2017-01-07T03:02:04Z
Comment #12 by issues.dlang — 2017-01-20T18:27:52Z
(In reply to greenify from comment #10)
> > All of these useless options left to sloppy range implementations don't bring value.
>
> This has probably been proposed before, but what about letting the compiler
> insert an opDollar alias if length is defined? This should greatly help to
> avoid sloppy implementation differences and one can still opt to use the
> opDollar overloads for superior performance.
https://issues.dlang.org/show_bug.cgi?id=7177 covers fixing it so that ranges that define length automatically get opDollar, but it's never gone anywhere. There has been discussion on it from time to time, but without implementing it, requiring ranges to work with $ is going to break a lot of code, since odds are that very few folks are implementing opDollar. Phobos itself doesn't do it consistently.
But it's pretty ridiculous that we can't use $ with anything but arrays (which is basically where we sit now), and it really should be fixed. I did fix it some time ago so that isRandomAccessRange and hasSlicing verify that $ works correctly if it works (at least verifies it as much as can be done statically), but they've never required that $ works, because so few ranges implement opDollar, and issue #7177 has never been implemented. I'd have changed isRandomAccessRange and hasSlicing to require that $ work years ago if it weren't for that.
Comment #13 by robert.schadek — 2024-12-01T16:26:56Z