Calling assumeSorted on const(std.container.Array)
Steven Schveighoffer
schveiguy at gmail.com
Sun Mar 26 02:16:15 UTC 2023
On 3/25/23 9:45 AM, Olivier Prat wrote:
> Would someone explain the exact nature of this error as I fail to
> understand as how just telling that an Array ConstRange is assumed to be
> sorted actually modifies anything??
It's because a Range keeps a copy of the array (Array is a reference
counted type). Since that is labeled `const`, then editing the Range is
forbidden.
Inside SortedRange, it has this, which is causing the issue:
```d
static if (isForwardRange!Range)
@property auto save()
{
// Avoid the constructor
typeof(this) result = this;
result._input = _input.save;
return result;
}
```
Overwriting the input isn't possible here, because it contains a const
member.
Now, this possibly could be fixed with something like:
`return typeof(this)(_input.save);`
But it might just push the error to another place.
The whole thing is kind of ugly.
There is a note inside the Array Range which says it's trying to work
around some old bug that is now marked as "works for me", so maybe that
can be reexamined.
https://github.com/dlang/phobos/blob/17b1a11afd74f9f8a69af93d77d4548a557e1b89/std/container/array.d#L137
-Steve
More information about the Digitalmars-d-learn
mailing list