I want to transmit the class name and the member name in the method

Brian zoujiaqing at gmail.com
Fri Jan 5 21:43:41 UTC 2018


On Friday, 5 January 2018 at 12:19:11 UTC, thedeemon wrote:
> On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote:
>> I think code style like:
>> db.select(User).where(email.like("*@hotmail.com")).limit(10);
>
> You need to read about templates in D, here's a good guide:
> https://github.com/PhilippeSigaud/D-templates-tutorial
>
> Basically you can write a function like
> auto select(Class, string fieldName)(Class value) {
>  ...
>
> and call it later as
> select!(User, "name")(u);
>
> Here User and "name" are compile-time arguments, you can pass 
> there types, values and more exotic stuff like other templates.
>
> And inside the function you can use
> __traits(getMember, u, fieldName)
> to get field by its name from the passed value.
> See: https://dlang.org/spec/traits.html

I can try it?:

auto users = 
db.select!(User).where(User.email.like("*@hotmail.com")).limit(10);


More information about the Digitalmars-d-learn mailing list