On 4/21/22 14:02, JG wrote:
> Could someone possibly help me to understand why the commented line
> doesn't compile?
iota(10).myMap!(x=>2*x).writeln;
It is because x=>2*x is just a template. I don't know why the compiler
chooses 'void' for typeof(f) but apparently that's how it represents the
types of function templates.
One way of changing the code is to use the following but it does not
really fit because of course you may want types other than 'int':
(int x) => 2 * x // Now the type is known
The more logical thing to do is to stay with alias template parameters
for MapResult as well. I am not sure why you need the member 'f' so I
commented it out but now it compiles after some trivial changes:
import std;
struct MapResult(R, alias f) // <== HERE
{
R r;
// const F f;
auto empty() { return r.empty; }
auto front() { return f(r.front); }
void popFront() { r.popFront; }
auto save() { return typeof(this)(r.save); }
}
auto myMap(alias f, R)(R r) {
pragma(msg, typeof(f));
return MapResult!(R, f)(r);
}
void main()
{
int function(int) f = x=>2*x;
iota(10).myMap!f.writeln;
iota(10).myMap!(x=>2*x).writeln;
}
Ali