www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get template name

reply "JS" <js.mdnq gmail.com> writes:
__FUNCTION__ does not return anything when used in templates.

For debugging purposes I sometimes use pragma(msg, 
template_name); (with template_name being the name of the 
template assigned an enum)

I would like to make this more general such as

pragma(msg, mixin(__FUNCTION_NAME__));

e.g.,

template t()
{
     enum t = "asdf";
     pragma(msg, t);
}

same but more general

template t()
{
    enum t = "asdf";
    pragma(msg, __FUNCTION_NAME__);
}

(please don't ask why I would do this... this is a simple 
example, my case is more complex, trust me that I have a reason)
Jul 29 2013
next sibling parent "evilrat" <evilrat666 gmail.com> writes:
On Monday, 29 July 2013 at 23:02:57 UTC, JS wrote:
 __FUNCTION__ does not return anything when used in templates.

 For debugging purposes I sometimes use pragma(msg, 
 template_name); (with template_name being the name of the 
 template assigned an enum)

 I would like to make this more general such as

 pragma(msg, mixin(__FUNCTION_NAME__));

 e.g.,

 template t()
 {
     enum t = "asdf";
     pragma(msg, t);
 }

 same but more general

 template t()
 {
    enum t = "asdf";
    pragma(msg, __FUNCTION_NAME__);
 }

 (please don't ask why I would do this... this is a simple 
 example, my case is more complex, trust me that I have a reason)
is this an option? writeln("template name: ", t.stringof);
Jul 29 2013
prev sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Monday, 29 July 2013 at 23:02:57 UTC, JS wrote:
 __FUNCTION__ does not return anything when used in templates.

 For debugging purposes I sometimes use pragma(msg, 
 template_name); (with template_name being the name of the 
 template assigned an enum)
It can be relatively trivial or incredibly difficult, depending on exact string output for a given template you want get and set of template types supported.
Jul 29 2013
parent reply "JS" <js.mdnq gmail.com> writes:
On Tuesday, 30 July 2013 at 06:36:12 UTC, Dicebot wrote:
 On Monday, 29 July 2013 at 23:02:57 UTC, JS wrote:
 __FUNCTION__ does not return anything when used in templates.

 For debugging purposes I sometimes use pragma(msg, 
 template_name); (with template_name being the name of the 
 template assigned an enum)
It can be relatively trivial or incredibly difficult, depending on exact string output for a given template you want get and set of template types supported.
I just want the current template name in the current scope. I know it seems trivial but it would help me reduce boilerplate code. I'm using it to debug and use the template name as a way to turn on debugging for specific templates if I need to analyze their behavior(lots of code generating templates that may have bugs). A similar feature would end up being needed for functions when I log functions debug output. With such a function I can just copy and paste the line to display the output rather than copy paste modify(replace the old template name with the new). I don't need the template name of a template that called it... that is different, possibly useful but not required. void f() { pragma(msg, __FUNCTION__); } template t() { pragma(msg, __FUNCTION__); } void main(string[] argv) { readln(); } the function displays main.f. The template displays nothing. I'd prefer it to display main.t! or something unique so I can use it as a hash.
Jul 29 2013
parent reply "Dicebot" <public dicebot.lv> writes:
On Tuesday, 30 July 2013 at 06:45:31 UTC, JS wrote:
 void f()
 {
 	pragma(msg, __FUNCTION__);
 }

 template t()
 {
     pragma(msg, __FUNCTION__);
 }

 void main(string[] argv)
 {
 	readln();
 }

 the function displays main.f. The template displays nothing. 
 I'd prefer it to display main.t! or something unique so I can 
 use it as a hash.
1) Pragma's are printed upon instantiation. You need to use something like alias _ = t!(); to force it. 2) You may workaround it by wrapping pragma in stub function. Though I do agree that something like __ENCLOSING__ may be useful (assuming it also covers aggregates)
Jul 30 2013
parent reply "JS" <js.mdnq gmail.com> writes:
On Tuesday, 30 July 2013 at 10:45:26 UTC, Dicebot wrote:
 On Tuesday, 30 July 2013 at 06:45:31 UTC, JS wrote:
 void f()
 {
 	pragma(msg, __FUNCTION__);
 }

 template t()
 {
    pragma(msg, __FUNCTION__);
 }

 void main(string[] argv)
 {
 	readln();
 }

 the function displays main.f. The template displays nothing. 
 I'd prefer it to display main.t! or something unique so I can 
 use it as a hash.
1) Pragma's are printed upon instantiation. You need to use something like alias _ = t!(); to force it. 2) You may workaround it by wrapping pragma in stub function. Though I do agree that something like __ENCLOSING__ may be useful (assuming it also covers aggregates)
1. No, the code actually will print the message but the error shows up right after(well, depends on which error, the one I get inside templates works fine except the without the assignment/alias, no further compilation is done due to the error(but the message is printed). And what is a stub function?
Jul 30 2013
parent reply "Dicebot" <public dicebot.lv> writes:
On Tuesday, 30 July 2013 at 11:04:10 UTC, JS wrote:
 1. No, the code actually will print the message but the error 
 shows up right after(well, depends on which error, the one I 
 get inside templates works fine except the without the 
 assignment/alias, no further compilation is done due to the 
 error(but the message is printed).
http://dpaste.dzfl.pl/e232e3ac
 And what is a stub function?
http://dpaste.dzfl.pl/133230f8
Jul 30 2013
next sibling parent "JS" <js.mdnq gmail.com> writes:
On Tuesday, 30 July 2013 at 11:25:38 UTC, Dicebot wrote:
 On Tuesday, 30 July 2013 at 11:04:10 UTC, JS wrote:
 1. No, the code actually will print the message but the error 
 shows up right after(well, depends on which error, the one I 
 get inside templates works fine except the without the 
 assignment/alias, no further compilation is done due to the 
 error(but the message is printed).
http://dpaste.dzfl.pl/e232e3ac
 And what is a stub function?
http://dpaste.dzfl.pl/133230f8
The first is what I already do, but it is messy. The second may work, it is also a bit messy(requiring an extra function) but might solve the problem...
Jul 30 2013
prev sibling parent "JS" <js.mdnq gmail.com> writes:
On Tuesday, 30 July 2013 at 11:25:38 UTC, Dicebot wrote:
 On Tuesday, 30 July 2013 at 11:04:10 UTC, JS wrote:
 1. No, the code actually will print the message but the error 
 shows up right after(well, depends on which error, the one I 
 get inside templates works fine except the without the 
 assignment/alias, no further compilation is done due to the 
 error(but the message is printed).
http://dpaste.dzfl.pl/e232e3ac
 And what is a stub function?
http://dpaste.dzfl.pl/133230f8
http://dpaste.dzfl.pl/c4cb0dfb For some reason the code doesn't work on my machine... works in dpaste though... My machine just returns an empty string for __FUNCTION__. I think this is a bug somewhere... I'll post another post showing it.
Jul 30 2013