Gotcha with const std.range.chunks

sportsracer steffenwenz+d at gmail.com
Thu Jul 30 18:52:42 UTC 2020


Newcomer to the D language here. I was going to use 
`std.range.chunks` to get a two-dimensional, read-only view on 
data in one continuous array.

But was surprised to find that this code doesn't compile:

import std.range : chunks;
import std.stdio : writeln;

void main()
{
     int[] xs = new int[100];
     const chunked = xs.chunks(10);
     writeln(chunked[0][0]);
}

Error: mutable method std.range.Chunks!(int[]).Chunks.opIndex is 
not callable using a const object

… I'm not sure if this is due to my ignorance of the D type 
system, or could be considered missing functionality in the 
chunks helper function.


More information about the Digitalmars-d-learn mailing list