A look inside "filter" function defintion

pascal111 judas.the.messiah.111 at gmail.com
Tue Aug 2 12:39:41 UTC 2022

On Tuesday, 2 August 2022 at 04:06:30 UTC, frame wrote:
> On Monday, 1 August 2022 at 23:35:13 UTC, pascal111 wrote:
>> This is the definition of "filter" function, and I think it 
>> called itself within its definition. I'm guessing how it works?
> It's a template that defines the function called "Eponymous 
> Templates":
> https://dlang.org/spec/template.html#implicit_template_properties
> A template generates code, it cannot be called, only 
> instantiated.
> The common syntax is just a shortcut for using it. Otherwise 
> you would need to write `filter!(a => a > 0).filter([1, -1, 2, 
> 0, -3])`. Like UFCS, some magic the compiler does for you.

Instantiation seems some complicated to me. I read "If a template 
contains members whose name is the same as the template 
identifier then these members are assumed to be referred to in a 
template instantiation:" in the provided link, but I'm still 
stuck. Do you have a down-to-earth example for beginners to 
understand this concept?

More information about the Digitalmars-d-learn mailing list