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

Philippe Sigaud philippe.sigaud at gmail.com
Tue Dec 10 10:40:38 PST 2013


> void main()
> {
>     Variant[] lols = [ Variant(["hello": Variant(1)]), Variant(["bye":
> Variant(true)])  ];
>     auto vtypes = map!(to!Variant[string])(lols); // <--- line 11
>
>     string[] filetypes = map!(to!string)(vtypes).array();
>     writeln(filetypes);
> }
>
> Gives me:
> main.d(11) Error: to!(VariantN!(24u)) is used as a type

As bearophile said, to!Variant[string]... is read as
to!(Variant)[string], which is not what you want. When a template
argument is more than one token long (Variant[string] has 4 tokens),
enclose it in parenthesis.

But here it will not help you, as I think the conversion you ask is
impossible: how could a Variant be transformed into a Variant[string]?
By definition of Variant, the compiler cannot know what is inside. The
first element of lol could be an a float wrapped into a Variant, for
example, and then how could it be transformed into Variant[string]?

Do you really need to enclose everything in Variants? Types are your
friends, you know :)

An array of Variant[string] would be far easier to work with:

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

void main()
{
    // See the type of lols
    Variant[string][] lols = [ ["hello": Variant(1)], ["bye": Variant(true)]  ];

    string[] filetypes = map!(to!string)(lols).array();
    writeln(filetypes);
}


More information about the Digitalmars-d-learn mailing list