compatible types for chains of different lengths

Jon D via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 17 14:47:16 PST 2015


I'd like to chain several ranges and operate on them. However, if 
the chains are different lengths, the data type is different. 
This makes it hard to use in a general way. There is likely an 
alternate way to do this that I'm missing.

A short example:

$ cat chain.d
import std.stdio;
import std.range;
import std.algorithm;

void main(string[] args)
{
     auto x1 = ["abc", "def", "ghi"];
     auto x2 = ["jkl", "mno", "pqr"];
     auto x3 = ["stu", "vwx", "yz"];
     auto chain1 = (args.length > 1) ? chain(x1, x2) : chain(x1);
     auto chain2 = (args.length > 1) ? chain(x1, x2, x3) : 
chain(x1, x2);
     chain1.joiner(", ").writeln;
     chain2.joiner(", ").writeln;
}
$ dmd chain.d
chain.d(10): Error: incompatible types for ((chain(x1, x2)) : 
(chain(x1))): 'Result' and 'string[]'
chain.d(11): Error: incompatible types for ((chain(x1, x2, x3)) : 
(chain(x1, x2))): 'Result' and 'Result'

Is there a different way to do this?

--Jon


More information about the Digitalmars-d-learn mailing list