Bug 6986 – SortedRange[x..$] fails with unidentified __dollar

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-11-21T22:35:00Z
Last change time
2014-01-26T09:11:41Z
Assigned to
nobody
Creator
jlquinn

Comments

Comment #0 by jlquinn — 2011-11-21T22:35:06Z
dmd 2.056 Linux x64 import std.range; void foo(int[] buf) { auto bufsort = assumeSorted(buf); auto r = bufsort[1..$]; } ~/d/dmd2/linux/bin64/dmd -c range.d range.d(5): Error: undefined identifier __dollar
Comment #1 by issues.dlang — 2011-11-21T22:57:17Z
Technically, this is an enhancement request, not a bug - though it's certainly a very desirable enhancement request. assumeSorted returns a SortedRange struct which wraps the range which was passed to it. SortedRange does not define opDollar, and at present, IIRC, opDollar hasn't been working correctly. So, it's not possible for SortedRange to define opDollar, and there's no way that your example is going to work. However, it looks like there was a commit made to dmd about an hour ago which relates to opDollar ( https://github.com/D-Programming-Language/dmd/commit/aba0f773416e1d45d227159cb22ad0e26bb980c0 ). So, it may now be possible to implement this. But the short answer for why this doesn't work is that $ doesn't yet work for anything other than built-in arrays, and assumeSorted doesn't return an array, even if you pass it one (and it would defeat the purpose of assumeSort if it did return an array).
Comment #2 by jlquinn — 2011-11-21T23:06:36Z
I'd think the simplest solution would be to do the same rewrite that happens for builtin arrays, i.e. x[p..$] => x.opSlice(p, x.length) Then no opDollar is needed.
Comment #3 by issues.dlang — 2011-11-21T23:13:04Z
It doesn't work that way. opDollar is what's supposed to be used. I forget all of the reasons why, but I believe that it has to do with cases where opDollar would do something more complicated than simply opSlice(p, x.length). opDollar is more flexible for some set of cases, but they're not the sort of use cases that I normally think of, so I don't remember the exact details at the moment. I'd have to go digging through the newsgroup archives for discussions which related to it.
Comment #4 by timon.gehr — 2011-11-22T03:49:48Z
The rewrite from $ to .length does not work if a range has no predetermined length (eg. if it is infinite or corresponds to an input stream)
Comment #5 by peter.alexander.au — 2014-01-26T09:11:41Z