__traits() to get parameter details only ? ... hasMember looks up everything within

Alexandru Ermicioi alexandru.ermicioi at gmail.com
Wed Aug 4 16:41:04 UTC 2021


On Wednesday, 4 August 2021 at 15:08:24 UTC, someone wrote:

> However, __traits(hasMember, ...) checks for the existence of 
> anything labeled lstrCurrencyID within the class (eg: unrelated 
> variables with same name; not gonna happen, but, I like to code 
> it the right way); so, question is: is there any way to search 
> the parameter declarations only ?
>
> Something akin to 
> https://dlang.org/spec/traits.html#getParameterStorageClasses 
> but for the parameter types/labels

What do you mean by parameter type?
Are you referring to @property functions or just whether there is 
a field in there?

In both cases you need to get that symbol with __traits(getMember 
...).
If it is about @property then check whether the symbol is a 
function and if so fetch the function attributes with  
functionAttributes template and check if it is marked as a 
property.

If this is about field of the class then you can test with 
FieldNameTuple.

Since you're using classes consider just declaring an interface 
that denotes that implementor has this field, it would be much 
easier to check for it, and easier for compiler since it avoids 
compile time magic.

Best regards,
Alexandru.


More information about the Digitalmars-d-learn mailing list