Function template declaration mystery...

Robert M. Münch robert.muench at saphirion.com
Wed Feb 28 20:27:28 UTC 2018


On 2018-02-28 18:09:41 +0000, H. S. Teoh said:

> Basically, the `alias f` is a catch-all template parameter that can
> bind to basically anything that has a symbol. It's typically used to
> bind to functions, delegates, and lambdas.

Aha... ok that makes it a bit more clear. So, if I have:

auto myFunc(alias f1, alias f2, alias f3, A)(auto ref A _a){...

I can use it like:

a.myFunc(
	(myType) {...myCode...},
	(myType) {...myCode...},
	(myType) {...myCode...}
);

and the aliases just continue to collect whatever comes inside the 
parameter specification. Does this idea hold? And it will give an error 
if there are to few/many aliases/parameters?

> Technically, the function is missing a sig constraint

How would that look like?

> that verifies thatf is in fact a unary function.  So it will fail to 
> compile if you pass
> something other than a unary function in.

Ok, that's clearn.

>> This function can be called with code like this:
>> 
>> a.do((myType) {...myCode...});
> 
> Are you sure the function name is 'do'? Because that's a keyword, and I
> don't think it's a valid identifier.

Well, it's just a bad picked word for my pseudo-code example... sorry.

>> do(a, (myType) {...myCode...});
> 
> Are you sure this actually works?  Is there another overload that takes
> a different parameter?  The overload with `alias f`, AFAIK, can only be
> called with a compile-time parameter, like this:
> 
> 	foo!((myType) { ... })(a);
> 
> 	// or:
> 	a.foo!((myType) { ... });

I'm trying to better understand the D reactive framework rx: 
https://github.com/lempiji/rx

This is the real code:

  osStream().filter!(wm => wm.message == WM_CREATE).doSubscribe((winMsg 
wm) {appInit();});
  doSubscribe(osStream().filter!(wm => wm.message == WM_CREATE), 
(winMsg wm) {appInit();});


>> 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.
> [...]
> 
> Are you sure there isn't an overload that takes a second parameter?
> Doesn't look like this will compile, given the above declaration.

To be honste I'm not sure, I try to understand the code. It's the 
rx.oberservable file.

Thanks a lot so far.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list