www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Exclamation symbol "!" within functions syntax

reply pascal111 <judas.the.messiah.111 gmail.com> writes:
I noticed more than once that the exclamation "!" is used within 
functions typing, and it seems like an operator with new use, for 
example "to!int()", ".tee!(l => sum += l.length)", 
"enforce!MissingArguments...", so what dose it means?
Jul 27 2022
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/27/22 04:00, pascal111 wrote:
 I noticed more than once that the exclamation "!" is used within 
 functions typing, and it seems like an operator with new use, for 
 example "to!int()", ".tee!(l => sum += l.length)", 
 "enforce!MissingArguments...", so what dose it means?
 
The binary ! operator is used for specifying template arguments. I have some explanation here: http://ddili.org/ders/d.en/templates.html#ix_templates.!,%20template%20instance Ali
Jul 27 2022
next sibling parent pascal111 <judas.the.messiah.111 gmail.com> writes:
On Wednesday, 27 July 2022 at 11:09:19 UTC, Ali Çehreli wrote:
 On 7/27/22 04:00, pascal111 wrote:
 I noticed more than once that the exclamation "!" is used 
 within functions typing, and it seems like an operator with 
 new use, for example "to!int()", ".tee!(l => sum += 
 l.length)", "enforce!MissingArguments...", so what dose it 
 means?
 
The binary ! operator is used for specifying template arguments. I have some explanation here: http://ddili.org/ders/d.en/templates.html#ix_templates.!,%20template%20instance Ali
I think I got it now, it's easy, I thought it so advanced D feature.
Jul 27 2022
prev sibling parent reply pascal111 <judas.the.messiah.111 gmail.com> writes:
On Wednesday, 27 July 2022 at 11:09:19 UTC, Ali Çehreli wrote:
 On 7/27/22 04:00, pascal111 wrote:
 I noticed more than once that the exclamation "!" is used 
 within functions typing, and it seems like an operator with 
 new use, for example "to!int()", ".tee!(l => sum += 
 l.length)", "enforce!MissingArguments...", so what dose it 
 means?
 
The binary ! operator is used for specifying template arguments. I have some explanation here: http://ddili.org/ders/d.en/templates.html#ix_templates.!,%20template%20instance Ali
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
Aug 01 2022
parent reply Paul Backus <snarwin gmail.com> writes:
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
Aug 01 2022
parent pascal111 <judas.the.messiah.111 gmail.com> writes:
On Monday, 1 August 2022 at 12:58:27 UTC, Paul Backus wrote:
 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
Ok! I'll read about that from the suggested sources, but with respect to Ali's book, he should put "More Templates" after the chapter of "templates", but he put "Pragmas" instead of that http://ddili.org/ders/d.en/pragma.html
Aug 01 2022