www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I the temlate parameter name as string?

reply PacMan <jckj33 gmail.com> writes:
ParameterIdentifierTuple from std.traits did work fine for 
regular functions but not for template functions:

 Error: template instance 
 `std.traits.ParameterIdentifierTuple!(f)` does not match 
 template declaration `ParameterIdentifierTuple(func...) if 
 (func.length == 1 && isCallable!func)` (foo)
f is defined as:
 void f(T)(T t, string p)
 if(is(T == A) || is(T == B))
{
	// ...
}
my goal is get "p" as string.
Nov 26 2018
next sibling parent Soulsbane <paul acheronsoft.com> writes:
On Tuesday, 27 November 2018 at 02:00:44 UTC, PacMan wrote:
 ParameterIdentifierTuple from std.traits did work fine for 
 regular functions but not for template functions:

 Error: template instance 
 `std.traits.ParameterIdentifierTuple!(f)` does not match 
 template declaration `ParameterIdentifierTuple(func...) if 
 (func.length == 1 && isCallable!func)` (foo)
f is defined as:
 void f(T)(T t, string p)
 if(is(T == A) || is(T == B))
{
	// ...
}
my goal is get "p" as string.
If I'm understanding right I need this quite frequently so I wrote this: template nameOf(alias nameType) { enum string nameOf = __traits(identifier, nameType); } unittest { immutable int name; assert(nameOf!name == "name"); }
Nov 26 2018
prev sibling next sibling parent Neia Neutuladh <neia ikeran.org> writes:
On Tue, 27 Nov 2018 02:00:44 +0000, PacMan wrote:
 f is defined as:
 
 void f(T)(T t, string p)
 if(is(T == A) || is(T == B))
{
	// ...
}
my goal is get "p" as string.
`f` is a template that generates functions. Before you instantiate that template, the function doesn't exist, so there's no function to get parameters from. You can get the parameter names of any instantiation of that template, such as: assert(ParameterIdentifierTuple!(f!A)[1] == "p");
Nov 26 2018
prev sibling parent bauss <jj_1337 live.dk> writes:
On Tuesday, 27 November 2018 at 02:00:44 UTC, PacMan wrote:
 ParameterIdentifierTuple from std.traits did work fine for 
 regular functions but not for template functions:

 Error: template instance 
 `std.traits.ParameterIdentifierTuple!(f)` does not match 
 template declaration `ParameterIdentifierTuple(func...) if 
 (func.length == 1 && isCallable!func)` (foo)
f is defined as:
 void f(T)(T t, string p)
 if(is(T == A) || is(T == B))
{
	// ...
}
my goal is get "p" as string.
p.stringof
Nov 27 2018