www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what's meaning of "(a) =>"

reply step8 <step7 163.com> writes:
I'm trying to study D programming
Following is code from vibe's example(web_ajax) code:
~~~~
void getDataFiltered(Fields field, string value)
{
	auto table = users.filter!((a) => value.length==0 || 
a[field]==value)().array();
	render!("createTable.dt", table)();
}
~~~~
I can't understand the expression "(a) =>",there is no defination 
of "a",how does this work?
thanks for help
Feb 05 2022
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Saturday, 5 February 2022 at 15:10:19 UTC, step8 wrote:
 I'm trying to study D programming
 Following is code from vibe's example(web_ajax) code:
 ~~~~
 void getDataFiltered(Fields field, string value)
 {
 	auto table = users.filter!((a) => value.length==0 || 
 a[field]==value)().array();
 	render!("createTable.dt", table)();
 }
 ~~~~
 I can't understand the expression "(a) =>",there is no 
 defination of "a",how does this work?
 thanks for help
`(a) => value.length==0 || a[field]==value` is a https://dlang.org/spec/expression.html#FunctionLiteral (see This one is polymorphic, somewhat equivalent to defining a function such as `bool func(T)(T a) { return value.length==0 || a[field]==value; }` (assuming `value` is accessible to `func`).
Feb 05 2022
parent step8 <step7 163.com> writes:
On Saturday, 5 February 2022 at 15:17:05 UTC, Stanislav Blinov 
wrote:
 On Saturday, 5 February 2022 at 15:10:19 UTC, step8 wrote:
 I'm trying to study D programming
 Following is code from vibe's example(web_ajax) code:
 ~~~~
 void getDataFiltered(Fields field, string value)
 {
 	auto table = users.filter!((a) => value.length==0 || 
 a[field]==value)().array();
 	render!("createTable.dt", table)();
 }
 ~~~~
 I can't understand the expression "(a) =>",there is no 
 defination of "a",how does this work?
 thanks for help
`(a) => value.length==0 || a[field]==value` is a https://dlang.org/spec/expression.html#FunctionLiteral This one is polymorphic, somewhat equivalent to defining a function such as `bool func(T)(T a) { return value.length==0 || a[field]==value; }` (assuming `value` is accessible to `func`).
Thanks very much.I think i kind of get that now.
Feb 06 2022