Function template declaration mystery...

TheFlyingFiddle none at none.com
Wed Feb 28 18:01:50 UTC 2018


On Wednesday, 28 February 2018 at 17:47:22 UTC, Robert M. Münch 
wrote:
> Hi, I'm lost reading some code:
>
> A a;
>
> auto do(alias f, A)(auto ref A _a){
> 	alias fun = unaryFun!f;
> 	return ...
> 	...
> }
>
> How is this alias stuff working? I mean what's the type of f? 
> Is it an anonymous function which then gets checked to be 
> unary? How is it recognized in the code using the function 
> template?
>
>
> This function can be called with code like this:
>
> a.do((myType) {...myCode...});
> do(a, (myType) {...myCode...});
>
> What's wondering me here is that the template function only has 
> one paraemter (_a) but I somehow can get my myCode into it. But 
> the code looks like a parameter to me. So why isn't it like:
>
> auto do(alias f, A)(auto ref A _a, ??? myCode){...
>
> I'm a bit confused.

Testing this with:

auto foo(alias f, A)(auto ref A a) { return f(a); }

I can call foo either like this:

foo!(x => x + x)(1);
or
1.foo!(x => x + x);

but these will give errors

foo(1, x => x + x); //Error
1.foo(x => x + x); // Error

I don't see how you can get that kind of behavior...




More information about the Digitalmars-d-learn mailing list