chain(const(array of class)) fails

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 31 16:19:44 PST 2016


On Sunday, 31 January 2016 at 21:22:06 UTC, SimonN wrote:
> Hi,
>
> we start with the following code snippet, which works.
>
>     import std.algorithm;
>     import std.range;
>     import std.stdio;
>
>     class A     { int val; }
>     class B : A { this() { val = 3; } }
>     class C : A { this() { val = 4; } }
>
>     B[] b = [new B(), new B()];
>     C[] c = [new C(), new C()];
>
>     void main()
>     {
>         chain(b, c).each!(a => a.val.writeln);
>     }
>
> The output, as expected, is:
>
>     3
>     3
>     4
>     4
>
> Now I change the declarations of B[] b and C[] c to the 
> following, keeping
> everything else in the code snippet the same:
>
>     const(B[]) b = [new B(), new B()];
>     const(C[]) c = [new C(), new C()];
>
> This makes dmd 2.070 choke: ( 
> http://dpaste.dzfl.pl/eee69fd03dd9 )
>
>     Error: template std.range.chain cannot deduce function from 
> argument
>     types !()(const(B[]), const(C[])),
>     candidates are: 
> /opt/compilers/dmd2/include/std/range/package.d(804):
>     std.range.chain(Ranges...)(Ranges rs) if (Ranges.length > 0 
> &&
>     allSatisfy!(iseputRange, staticMap!(Unqual, Ranges)) && 
> !is(CommonType!(
>     staticMap!(ElementType, staticMap!(Unqual, Ranges))) == 
> void))
>
> What's stumping me -- constness doesn't make dmd choke on 
> ranges of
> numbers. If I replace the classes B and C with simple 'int' and 
> 'double',
> this compiles again:
>
>     const(int[])    b = [1, 2];
>     const(double[]) c = [3.3, 4.4];
>     void main() { chain(b, c).each!(a => a.writeln); }
>
> Why does it fail for const(array of class)?
> Is any template magic about Unqual or staticMap relevant here?
>
> -- Simon

  types !()(const(B[]), const(C[])),
>     candidates are: 
> /opt/compilers/dmd2/include/std/range/package.d(804):
>     std.range.chain(Ranges...)(Ranges rs) if (Ranges.length > 0 
> &&
>     allSatisfy!(iseputRange, staticMap!(Unqual, Ranges)) && 
> !is(CommonType!(
>     staticMap!(ElementType, staticMap!(Unqual, Ranges))) == 
> void))

Unqaul means remove any const or immutable torn the type
StaticMap is like a compile time map

What this error message says is that there is one candidate 
function that matches what you are attempting to do. and that 
chain takes a variadic argument and each of those arguments must

1) when unqualified be an input range (Basically you can foreach 
over it)
2) that the common type of the element type of the unqualified 
variadic argument types is not void (in this case not arrays of 
void)

Have you tried changing The declaration of a and b to const(A[])?

Also have you tried with other reference type (e.g. assoc arrays 
pointers)?

Nic


More information about the Digitalmars-d-learn mailing list