Less typed ranges

bearophile bearophileHUGS at lycos.com
Sat Feb 4 07:49:39 PST 2012


You can't write this:


import std.algorithm;
void main() {
    int[] a1 = [1, 2, 3];
    auto r1 = map!(x => 2 * x)(a1);
    auto r2 = map!(x => x ^^ 2)(a1);
    auto a2 = [r1, r2];
}


because the types of r1 and r2 aren't compatibile:
test.d(6): Error: incompatible types for ((r1) ? (r2)): 'Result' and 'Result'


So is it good to have something like this in Phobos that uses some type erasure?


import std.algorithm, std.range;
void main() {
    int[] a1 = [1, 2, 3];
    ForwardRange!int r1 = forwardRange(map!(x => 2 * x)(a1));
    ForwardRange!int r2 = forwardRange(map!(x => x ^^ 2)(a1));
    ForwardRange!int[] a2 = [r1, r2];
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list