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

Era Scarecrow via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 25 12:07:32 PDT 2016


On Wednesday, 25 May 2016 at 13:27:28 UTC, Chris wrote:
> Why can the tuple be iterated with foreach, as in my quick fix, 
> and indexed with tuple[0..], but is not accepted as a range? 
> What are the differences? Is there a way to rangify a tuple?

  The tuple is identified/used at compile-time, as such it's a 
compiler primitive and not a range. Foreach in this case will 
unroll the loop regardless how it looks. So...

   test(Args...)(Args args) {
   ...
   foreach (const ref i; items)
     itemstrings ~= i.to!string;

   Will become: (const and ref are pointless in this example, 
unless the args are referenced)

   test(int arg1, int arg2, int arg3, int arg4) {
   ...
     itemstrings ~= arg1.to!string;
     itemstrings ~= arg2.to!string;
     itemstrings ~= arg3.to!string;
     itemstrings ~= arg4.to!string;


  Trying to use map on it was literally expanding the entire input 
to map.

   itemstrings => map!(to!string)(items);

   will become (all as individual items, not an array or range):

   itemstrings => map!(to!string)(arg1, arg2, arg3, arg4);

   You could probably use Array() inside which might work, 
assuming they are all the same type (or does it support a 
multi-type? I don't think so, unless you use Variant).

   There's a good section on Homogeneous vs Variadic behavior. 
Starts on page TDPL pg. 159-164

  If you had the function declared as: test(int[] args...) then 
numbers passed with or without an array would be converted as an 
array. so your calling function would have an array which can be 
used with map, however it loses it's flexible template and 
variable types...


More information about the Digitalmars-d-learn mailing list