Using std.algorithm.map: Error: cannot implicitly convert expression of type MapResult!
Dfr
deflexor at yandex.ru
Tue Dec 10 09:23:01 PST 2013
Thank you for good explanation.
But i currently hit little more complex case and again stuck,
maybe some ideas how to resolve this, unfortunately compiler
messages not very explanatory.
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
On Tuesday, 10 December 2013 at 06:05:50 UTC, Philippe Sigaud
wrote:
> 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