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

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


On Wednesday, 25 May 2016 at 10:24:19 UTC, pineapple wrote:
> On Tuesday, 24 May 2016 at 20:18:34 UTC, Steven Schveighoffer 
> wrote:
>> Slice assignment from range to array is not supported.
>>
>> In your example, I'm curious why the efforts to specify the 
>> type? I think it would work with just saying auto itemstrings 
>> = ...
>>
>> -Steve
>
> I still get an error if I use auto instead.

If you really need it, this works:

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

string test(Args...)(in Args items)
{
	writeln(items.stringof);
	string[] itemstrings;
	foreach (const ref i; items)
		itemstrings ~= i.to!string;
	// auto itemstrings = items.map!(a => a.to!string);
   return join(itemstrings, ", ");
}

unittest
{
   writeln(test(1, 2, "v", 4, 'c'));
}

If you use map!(), you get this error:

Error: template map_error.test!(int, int, string, int, 
char).test.map!((a) => a.to!string).map cannot deduce function 
from argument types !()(const(int), const(int), const(string), 
const(int), const(char)), candidates are:
/home/christoph/.dvm/compilers/dmd-2.071.0/linux/bin/../../src/phobos/std/algorithm/iteration.d(450):        map_error.test!(int, int, string, int, char).test.map!((a) => a.to!string).map(Range)(Range r) if (isInputRange!(Unqual!Range))

The argument types don't match, i.e. they are const(int) etc. 
instead of int. The arguments are passed as a tuple:

cf. writeln(items.stringof);
tuple(_param_0, _param_1, _param_2, _param_3, _param_4)




More information about the Digitalmars-d-learn mailing list