What's wrong with my usage of std.algorithm.map in this code example?

FreeSlave via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 25 04:14:26 PDT 2016


On Tuesday, 24 May 2016 at 20:03:14 UTC, pineapple wrote:
> I would've expected this to work, but instead I get a compile 
> error. Is my syntax wrong? Is this just not a case that map can 
> handle, and I should be doing something else?
>
>     import std.algorithm : map;
>     import std.conv : to;
>     import std.stdio : writeln;
>     import std.string : join;
>
>     string test(Args...)(in Args items){
>         immutable string[items.length] itemstrings = 
> map!(to!string)(items);
>         return join(itemstrings, ", ");
>     }
>
>     unittest{
>         writeln(test(1, 2, 3, 4));
>     }

Works with 'only', 'array' and static array slicing.

import std.algorithm : map;
import std.range : only;
import std.conv : to;
import std.stdio : writeln;
import std.string : join;
import std.array : array;

string test(Args...)(in Args items){
     immutable string[items.length] itemstrings = 
map!(to!string)(only(items)).array;
     return join(itemstrings[], ", ");
}

unittest{
     writeln(test(1, 2, 3, 4));
}


More information about the Digitalmars-d-learn mailing list