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

Dfr deflexor at yandex.ru
Tue Dec 10 22:26:17 PST 2013


Thank you, this clears thing to me.

I currently has all stuff wrapped in Variants because it is 
structure parsed from xml (or json), and it could be for example 
map of Something[string], where Something could be string or 
array or another map, and also this all nested few levels deep. 
I'm new to D and maybe there is better solution exists how to 
represent such structure ?


On Tuesday, 10 December 2013 at 18:40:48 UTC, Philippe Sigaud 
wrote:
>> 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