Using std.algorithm.map: Error: cannot implicitly convert expression of type MapResult!

Philippe Sigaud philippe.sigaud at gmail.com
Mon Dec 9 22:05:38 PST 2013


On Tue, Dec 10, 2013 at 6:54 AM, Dfr <deflexor at yandex.ru> wrote:
> Hello, here is example code, which doesn't work:
>
>     Variant[] vtypes = [ Variant("hello"), Variant("bye") ];
>     string[] filetypes = map!(to!string)(vtypes);
>
> Gives me error:
>
> Error: cannot implicitly convert expression (map(vtypes)) of type
> MapResult!(to, VariantN!(24u)[]) to string[]

> What is wrong here and how to fix it ?

map, and with it most other algorithm and ranges in std.algorithm and
std.range, returns lazy ranges: structs that will produce the data you
want when you iterate on them (with foreach, for example).

What map!(to!string)(someArray) does is constructing a 'view' on
someArray that will get you someArray elements, concerted into string.

If you want to convert it into an array, use std.array.array:

import std.stdio;
import std.algorithm;
import std.range;
import std.array;
import std.conv;
import std.variant;

void main()
{
    Variant[] vtypes = [ Variant("hello"), Variant("bye"), Variant(3.1415) ];
    string[] filetypes = map!(to!string)(vtypes).array();
    writeln(filetypes);
}


More information about the Digitalmars-d-learn mailing list