std.algorithm.map with multiple lambdas (2.066.0-b1) (does not compile)
klasbo via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 5 08:51:30 PDT 2014
void main(){
import std.algorithm, std.stdio;
auto arr = [1,2,3];
arr.map!("a + a", "a * a").writeln; //compiles
arr.map!(a => a + a, a => a * a).writeln; //does not
}
If I define two functions outside main, it works:
void main(){
import std.algorithm, std.stdio;
auto arr = [1,2,3];
arr.map!(twoTimes, square).writeln;
}
int square(int i){ return i*i; }
int twoTimes(int i){ return i+i; }
However, if `twoTimes` and `square` are nested inside of main(),
it fails.
[...]\typetuple.d(550): Error: template instance F!(twoTimes)
cannot use local 'twoTimes' as parameter to non-global template
AppliedReturnType(alias f)
[...]\typetuple.d(556): Error: template instance
maptest.main.staticMap!(AppliedReturnType, twoTimes) error
instantiating
[...]\algorithm.d(415): instantiated from here:
staticMap!(AppliedReturnType, twoTimes, square)
.\maptest.d(8): instantiated from here: map!(int[])
The alias declaration
`alias AppliedReturnType(alias f) = typeof(f(r.front));`
... is something I have never seen before.
So I hardcoded/expanded this line:
`alias ReturnTypes = TypeTuple!(AppliedReturnType!(_funs[0]),
AppliedReturnType!(_funs[1]));`
... which gives me the same error (cannot use local as parameter
to non-global). So I threw it in a pragma(msg, [...]):
`pragma(msg, TypeTuple!(AppliedReturnType!(_funs[0]),
AppliedReturnType!(_funs[1])));`
... which printed `(int, int)`, as expected. And also the same
error.
Wat.
More information about the Digitalmars-d-learn
mailing list