chain(const(array of class)) fails

SimonN via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 1 12:27:32 PST 2016


Sorry for late reply -- but I got around to test a couple more 
cases!

On Monday, 1 February 2016 at 00:19:44 UTC, Nicholas Wilson wrote:
> Unqaul means remove any const or immutable torn the type

Okay, that sounds like our 'const' shouldn't matter. 'const' is 
the outermost qualifier, and stripping that leaves us with B[] 
and C[], which were chainable earlier.

> 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)

Yep, const(B[]) and const(C[]) can be foreached. My workaround 
has been to replace chain() with several foreaches.

> 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[])?

Surprisingly, this compiles and gives the desired output:

     const(B[]) b = [ new B(), new B() ];
     const(A[]) c = [ new C(), new C() ]; // A instead of C

     chain(b, c).each!(a => a.val.writeln);

With two arguments, const(array) has worked iff at least one 
range is of the base type. Only if none were of the base type, I 
got the error.

Apparently, the template is smart enough to infer the common base 
type without 'const', but needs to be fed the basetype in case of 
'const'.

My gut feeling is that I should report this as a bug against 
phobos...

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

     immutable(B[int]) b;
     immutable(C[int]) c;

     chain(b.byValue, c.byValue).each!(a => a.val.writeln);

Error is the same as for the classes:

     template std.range.chain cannot deduce function
     from argument types !()(Result, Result), candidates are: /* 
snip */

To get this error, again, if at least one range is 
'immutable(A[int]).byValue', i.e., using the base class A, the 
template instantiates with no problems.

-- Simon


More information about the Digitalmars-d-learn mailing list