Append to 'map' result
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jul 4 17:28:01 PDT 2017
On 07/04/2017 04:57 PM, Jean-Louis Leroy wrote:
> On Tuesday, 4 July 2017 at 23:26:28 UTC, H. S. Teoh wrote:
>> On Tue, Jul 04, 2017 at 11:27:25PM +0000, Jean-Louis Leroy via
>> Digitalmars-d-learn wrote:
>>> I want to create a range that consists of the result of a map()
>>> followed by a value, e.g.:
>>>
>>> int[] x = [ 1, 2, 3];
>>> auto y = map!(x => x * x)(x);
>>> auto z = y ~ 99; // how???
>>>
>>> I have tried several variations: convert 99 to a dynamic array, to a
>>> range, convert range to dynamic array (couldn't even figure that
>>> one); to no avail.
>> [...]
>>
>> Try:
>>
>> auto z = chain(y, only(99));
>
> Thanks!
>
> However, when I try to use this construct in my real code, I still have
> a problem:
>
> // k is a ClassInfo
> chain(map!(i => i.classinfo)(k.interfaces), only(k.base))
>
> meth.d(311): Error: template std.range.chain cannot deduce function from
> argument types !()(MapResult!(__lambda3, Interface[]),
> OnlyResult!(TypeInfo_Class, 1LU)), candidates are:
> /usr/include/d/std/range/package.d(795):
> std.range.chain(Ranges...)(Ranges rs) if (Ranges.length > 0 &&
> allSatisfy!(isInputRange, staticMap!(Unqual, Ranges)) &&
> !is(CommonType!(staticMap!(ElementType, staticMap!(Unqual, Ranges))) ==
> void))
>
>
>
No time to dig deeper but this is because the two ranges that chain()
receives do not have a common type. (Rather, that type is 'void'):
import std.algorithm;
import std.range;
import std.traits;
class C {
}
void main() {
auto k = C.classinfo;
auto m = map!(i => i.classinfo)(k.interfaces);
auto b = only(k.base);
// Types
alias M = typeof(m);
alias B = typeof(b);
// Element types
alias EM = ElementType!M;
alias EB = ElementType!B;
// Two of chain's constraints are satisfied:
static assert(isInputRange!M);
static assert(isInputRange!B);
// Now it needs to see a CommonType of range elements
pragma(msg, EM); // const(void)[]()
pragma(msg, EB); // const(void)[]()
pragma(msg, CommonType!(EM, EB)); // void
// For that reason, the following does not work:
// chain(m, b);
}
Ali
More information about the Digitalmars-d-learn
mailing list