www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get parameter names

reply EntangledQuanta <EQ universe.com> writes:
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.
Sep 01 2017
next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Friday, September 01, 2017 20:58:20 EntangledQuanta via Digitalmars-d-
learn 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.
Those would actually be the arguments, not the parameters (c would be the parameters), but regardless, there's no way to do that. The function being called knows nothing about the caller. The only case that's even vaguely like that are the __FILE__ and __LINE__ directives which evaluate at the call site rather at the declaration site if they're used to default initialize a parameter (whereas in C++, they evaluate at the declaration site, which is a lot less useful). The function knows what its parameters are, but it knows nothing about what the arguments that were used to initialize the parameters were. If you want the names of the arguments to be passed to the function, you're going to have to pass them yourself. - Jonathan M Davis
Sep 01 2017
prev sibling parent reply Biotronic <simen.kjaras gmail.com> writes:
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
Sep 01 2017
parent EntangledQuanta <EQ universe.com> writes:
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).
Sep 01 2017