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

someone someone at somewhere.com
Wed Aug 4 15:08:24 UTC 2021


I have the following chunk of code that needs to conditionally 
instantiate existing classes (at compilation time) named 
classTickerCustom{ExchangeID} with one or ... two parameters (if 
any); eg:

```d
/// securities trading on exchange primary currency; eg: USD

... = new classTickerCustomNYSE("AMC");
... = new classTickerCustomNASDAQ("TSLA");

/// securities trading on exchange secondary currency; eg: EUR

... = new classTickerCustomNYSE("XXX", "EUR");
... = new classTickerCustomNASDAQ("YYY", "EUR");

/// not real cases ... of course
```

And I already have a working solution using:

```d
static if (__traits(hasMember, "...", "...") == true) {

    ...

}
```

More specifically:

```d
...

mixin(format!

    ` /// code chunk

    static if (__traits(hasMember, r"classTickerCustom%1$s"d, 
r"lstrCurrencyID"d) == true) { /// checking whether target class 
has an optional second parameter labeled lstrCurrencyID:

       classTickerCustom%1$s lobjTickerCustom%1$s = new 
classTickerCustom%1$s(
          ludtTicker.symbolID,
          ludtTicker.currencyID /// overriding exchange primary 
currency ID with given one
          );

    } else {

       classTickerCustom%1$s lobjTickerCustom%1$s = new 
classTickerCustom%1$s(ludtTicker.symbolID);

    }

    `(sudtExchange.ID) /// code chunk

); /// mixin ending

...
```

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


More information about the Digitalmars-d-learn mailing list