Exclamation symbol "!" within functions syntax
Paul Backus
snarwin at gmail.com
Mon Aug 1 12:58:27 UTC 2022
On Monday, 1 August 2022 at 11:35:25 UTC, pascal111 wrote:
> I noticed that filter is using the concept of templates but
> this time it's with a lambda function, not with a data type,
> how can we explain this? isn't supposed to use a data type
> after the exclamation mark: "auto r = chain(a, b).filter!(a =>
> a > 0);"
>
> https://dlang.org/phobos/std_algorithm_iteration.html#.filter.filter
If you look at the documentation for the filter template [1], you
will notice that the "predicate" parameter is marked with the
keyword "alias":
template filter(alias predicate)
Alias template parameters are special. They can accept not only
types, but also values, names of variables, and even things like
modules and packages as arguments. You can read more about them
in the "More Templates" chapter of Ali's book [2], and in the
official D language specification [3].
[1] https://dlang.org/phobos/std_algorithm_iteration.html#.filter
[2]
http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.alias,%20template%20parameter
[3] https://dlang.org/spec/template.html#aliasparameters
More information about the Digitalmars-d-learn
mailing list