Gotcha with const std.range.chunks
sportsracer
steffenwenz+d at gmail.com
Fri Jul 31 15:37:22 UTC 2020
Right, thank you! The range object needs to be mutated to be
iterated on, that explains it.
Although it looks like the opIndex on this particular range could
guarantee constness:
https://github.com/dlang/phobos/blob/v2.093.0/std/range/package.d#L8099
Here's a longer code snippet that gives some context on why I
thought I needed a const range object. This Matrix class would
have the chunks range object stored as a private property, and
use it in its opIndex to look up values. I wanted to add the
const qualifier to the opIndex method, since it doesn't mutate
the Matrix state, and that's what surfaced this error.
class Matrix(T)
{
private {
T[] data;
Chunks!(T[]) view;
}
this(size_t width, size_t height)
{
this.data = new int[width * height];
this.view = data.chunks(width);
}
T opIndex(size_t col, size_t row) /*const*/ // FIXME Can't be
const because Chunks.opIndex isn't
{
return view[row][col];
}
}
void main()
{
auto matrix = new Matrix!int(10, 10);
writeln(matrix[0, 0]);
}
Oh well, I'll try a different way!
More information about the Digitalmars-d-learn
mailing list