www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15480] New: std.algorithm.iteration.map not accepting

https://issues.dlang.org/show_bug.cgi?id=15480

          Issue ID: 15480
           Summary: std.algorithm.iteration.map not accepting multiple
                    lambdas
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: tir.karthi gmail.com

The map function in std.algorithm.iteration only accepts strings with "a" as
the variable name for multiple functions at
https://dlang.org/phobos/std_algorithm_iteration.html#.map . The following code
doesn't work with two lambdas being passed to map as follows : 

Program

import std.algorithm.iteration : map;
import std.algorithm.comparison : equal;
import std.range : chain;

void main() {
  int[] arr1 = [ 1, 2, 3, 4 ];
  int[] arr2 = [ 5, 6 ];
  auto dd = map!(z => z * z, c => c * c * c)(chain(arr1, arr2));
}

Output : 

/usr/include/dmd/phobos/std/meta.d(546): Error: template instance F!(__lambda1)
cannot use local '__lambda1' as parameter to non-global template
AppliedReturnType(alias f)
/usr/include/dmd/phobos/std/meta.d(552): Error: template instance
maps_square.main.staticMap!(AppliedReturnType, __lambda1) error instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(447):        instantiated
from here: staticMap!(AppliedReturnType, __lambda1, __lambda2)
maps_square.d(11):        instantiated from here: map!(Result)
/usr/include/dmd/phobos/std/meta.d(546): Error: template instance F!(__lambda2)
cannot use local '__lambda2' as parameter to non-global template
AppliedReturnType(alias f)
/usr/include/dmd/phobos/std/meta.d(553): Error: template instance
maps_square.main.staticMap!(AppliedReturnType, __lambda2) error instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(447):        instantiated
from here: staticMap!(AppliedReturnType, __lambda1, __lambda2)
maps_square.d(11):        instantiated from here: map!(Result)

The same program when replaced with mixins instead of functions with variable
name as "a" works

Program : 

import std.algorithm.iteration : map;
import std.algorithm.comparison : equal;
import std.range : chain;

void main() {
  int[] arr1 = [ 1, 2, 3, 4 ];
  int[] arr2 = [ 5, 6 ];
  auto dd = map!("a * a", "a * a * a")(chain(arr1, arr2));
}


The relevant discussion at dlang learn forum at
http://forum.dlang.org/thread/cujttuzdgxhtwowyopjj forum.dlang.org

I tried reading the source
https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L520
.Some hint that "a" should be used at
https://github.com/D-Programming-Language/phobos/blob/master/std/functional.d#L101.
It will be helpful to specify lambdas instead of mixins with "a" variable names
as they are more readable and elegant. The workaround was to use tuples in the
forum. I am d beginner and any thoughts on confirming this as a bug will be
helpful.

System information : 

OS : Linux Mint 15 (64 bit)
DMD version : DMD64 D Compiler v2.069.2

--
Dec 27 2015