get parameter names

EntangledQuanta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 1 15:36:11 PDT 2017


On Friday, 1 September 2017 at 22:21:18 UTC, Biotronic wrote:
> On Friday, 1 September 2017 at 20:58:20 UTC, EntangledQuanta 
> wrote:
>>
>> template(A, B...)
>> {
>>    auto foo(C...)(C c)
>>    {
>>        ... get c's parameter names, should be alpha, beta
>>    }
>> }
>>
>>
>> foo!(., .)(alpha, beta)
>>
>> I need the actual identifiers passed to foo. I can get the 
>> types(obviously C) but when I try to get the identifier 
>> names(__traits(identifier or other methods) I stuff get 
>> _param_k or errors.
>>
>> I need both C's types and the parameter identifier names past, 
>> else I'd just pass as strings.
>
> Like Jonathan M Davis points out, this is impossible for 
> regular parameters. For template alias parameters, on the other 
> hand, this works:
>
> void bar(alias fn)() {
>     assert(fn.stringof == "alpha");
> }
>
> unittest {
>     int alpha;
>     bar!(alpha);
> }
>
> --
>   Biotronic

The problem I have with this is that when I try to pass variables 
in the template complains that there is no "this"

So, what I have resorted to doing is passing the type and the 
name, which seems redundant:

bar!(int, "alpha")

rather than

bar!(alpha) or bar(alpha)

alpha is a variable in a object in my case.

I've tried basically something like the following

void bar(alias fn)()
{
    typeof(fn) should return int and
    fn.stringof should return "alpha"
}

although my code is more complex since I have multiple template 
parameters(using a variadic).


More information about the Digitalmars-d-learn mailing list