Why can't we compute the cartesian product of two immutable ranges ?
The following code :
import std.stdio;
import std.algorithm;
void main() {
immutable int[] B = [ 1, 2, 3 ];
immutable int[] C = [ 4, 5, 6 ];
auto BC = cartesianProduct(B, C);
writeln(BC);
}
Throws :
/usr/include/dmd/phobos/std/range.d(4199): Error: cannot modify struct result._ranges_field_1 Repeat!(immutable(int)) with immutable members
/usr/include/dmd/phobos/std/range.d(4503): Error: template instance std.range.Zip!(immutable(int)[], Repeat!(immutable(int))) error instantiating
/usr/include/dmd/phobos/std/algorithm.d(11674): instantiated from here: zip!(immutable(int)[], Repeat!(immutable(int)))
laurent_test.d(8): instantiated from here: cartesianProduct!(immutable(int)[], immutable(int)[])
/usr/include/dmd/phobos/std/algorithm.d(11674): Error: template instance std.range.zip!(immutable(int)[], Repeat!(immutable(int))) error instantiating
laurent_test.d(8): instantiated from here: cartesianProduct!(immutable(int)[], immutable(int)[])
laurent_test.d(8): Error: template instance std.algorithm.cartesianProduct!(immutable(int)[], immutable(int)[]) error instantiating
Futhermore, if the second but the first immutable is removed it works.
According to the phobos implementation, one of the range as to be an inputRange and the other a forwardRange. Why such template constraints ?
I'm definitely not an expert in D, but I asked a similar question last year and this answer from Jonathan M Davis was excellent.
TL;DR: A range can't be immutable because it would't respect the 4 rules:
Can you guess the culprint?
popFront