digitalmars.D - Get name of alias parameter at compile time?
- dsimcha (8/8) Oct 13 2009 Is there a way to get the name of an alias parameter at compile time? F...
- Jacob Carlborg (2/10) Oct 14 2009 Do you want that to print "fun" instead of "doStuff"?
- dsimcha (2/15) Oct 15 2009 No, the whole point is that I want to print "doStuff".
- Denis Koroskin (13/29) Oct 15 2009 What's the big deal?
- Lutger (18/56) Oct 15 2009 I was a bit surprised too since the code dsimcha posted did exactly that...
- dsimcha (4/60) Oct 15 2009 Yeah, now that I look into it further, what you describe is exactly the ...
- Lutger (13/18) Oct 15 2009 See my reply to my reply (sorry!) for a better way to make it work.
- Jacob Carlborg (2/20) Oct 15 2009 See my reply to dsimcha
- Jacob Carlborg (5/65) Oct 15 2009 Oh, that problem. Just use a function pointer and get the name of that
- Lutger (6/11) Oct 15 2009 Right, good to know LDC does get it right.
- Lutger (7/7) Oct 15 2009 Bah, I replied too soon. These also work, makes sense now I think about ...
Is there a way to get the name of an alias parameter at compile time? For example: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }
Oct 13 2009
On 10/14/09 06:36, dsimcha wrote:Is there a way to get the name of an alias parameter at compile time? For example: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }Do you want that to print "fun" instead of "doStuff"?
Oct 14 2009
Jacob Carlborg Wrote:On 10/14/09 06:36, dsimcha wrote:No, the whole point is that I want to print "doStuff".Is there a way to get the name of an alias parameter at compile time? For example: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }Do you want that to print "fun" instead of "doStuff"?
Oct 15 2009
On Thu, 15 Oct 2009 18:22:37 +0400, dsimcha <dsimcha yahoo.com> wrote:Jacob Carlborg Wrote:What's the big deal? import std.stdio; void doStuff() { } void templ(alias fun)() { writeln(fun.stringof); // prints "main()" } void main() { templ!(main); } Works for both D1 and D2On 10/14/09 06:36, dsimcha wrote:No, the whole point is that I want to print "doStuff".Is there a way to get the name of an alias parameter at compiletime? Forexample: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }Do you want that to print "fun" instead of "doStuff"?
Oct 15 2009
Denis Koroskin wrote:On Thu, 15 Oct 2009 18:22:37 +0400, dsimcha <dsimcha yahoo.com> wrote:I was a bit surprised too since the code dsimcha posted did exactly that. However, change void doStuff() to void doStuff(int a) and you got an error. I remembered hacking around that one, if anybody knows how to do it better it would be good to know: void doStuff(int a) { // Do stuff. } void templ(T...)() if (T.length==1) { writeln( T.stringof[6..$-1] ); } void main() { templ!doStuff(); }Jacob Carlborg Wrote:What's the big deal? import std.stdio; void doStuff() { } void templ(alias fun)() { writeln(fun.stringof); // prints "main()" } void main() { templ!(main); } Works for both D1 and D2On 10/14/09 06:36, dsimcha wrote:No, the whole point is that I want to print "doStuff".Is there a way to get the name of an alias parameter at compiletime? Forexample: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }Do you want that to print "fun" instead of "doStuff"?
Oct 15 2009
== Quote from Lutger (lutger.blijdestijn gmail.com)'s articleDenis Koroskin wrote:Yeah, now that I look into it further, what you describe is exactly the problem. The obvious way only works for functions w/o parameters. I simplified my example before I posted it and never bothered to test it.On Thu, 15 Oct 2009 18:22:37 +0400, dsimcha <dsimcha yahoo.com> wrote:I was a bit surprised too since the code dsimcha posted did exactly that. However, change void doStuff() to void doStuff(int a) and you got an error. I remembered hacking around that one, if anybody knows how to do it better it would be good to know: void doStuff(int a) { // Do stuff. } void templ(T...)() if (T.length==1) { writeln( T.stringof[6..$-1] ); } void main() { templ!doStuff(); }Jacob Carlborg Wrote:What's the big deal? import std.stdio; void doStuff() { } void templ(alias fun)() { writeln(fun.stringof); // prints "main()" } void main() { templ!(main); } Works for both D1 and D2On 10/14/09 06:36, dsimcha wrote:No, the whole point is that I want to print "doStuff".Is there a way to get the name of an alias parameter at compiletime? Forexample: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }Do you want that to print "fun" instead of "doStuff"?
Oct 15 2009
dsimcha wrote:Yeah, now that I look into it further, what you describe is exactly the problem. The obvious way only works for functions w/o parameters. I simplified my example before I posted it and never bothered to test it.See my reply to my reply (sorry!) for a better way to make it work. It is because of this I think: void foo() {} void bar(int) {} void main() { writeln(foo.stringof); // ok writeln(bar.stringof); // error: not callable using argument types () writeln(bar(1).stringof); // ok } It should be doable to make a template for getting the proper name of any aliased function I think.
Oct 15 2009
On 10/15/09 18:06, Lutger wrote:dsimcha wrote:See my reply to dsimchaYeah, now that I look into it further, what you describe is exactly the problem. The obvious way only works for functions w/o parameters. I simplified my example before I posted it and never bothered to test it.See my reply to my reply (sorry!) for a better way to make it work. It is because of this I think: void foo() {} void bar(int) {} void main() { writeln(foo.stringof); // ok writeln(bar.stringof); // error: not callable using argument types () writeln(bar(1).stringof); // ok } It should be doable to make a template for getting the proper name of any aliased function I think.
Oct 15 2009
On 10/15/09 17:55, dsimcha wrote:== Quote from Lutger (lutger.blijdestijn gmail.com)'s articleOh, that problem. Just use a function pointer and get the name of that instead, like this: http://www.dsource.org/projects/dstep/browser/dstep/internal/Traits.d (functionNameOf at line 17)Denis Koroskin wrote:Yeah, now that I look into it further, what you describe is exactly the problem. The obvious way only works for functions w/o parameters. I simplified my example before I posted it and never bothered to test it.On Thu, 15 Oct 2009 18:22:37 +0400, dsimcha<dsimcha yahoo.com> wrote:I was a bit surprised too since the code dsimcha posted did exactly that. However, change void doStuff() to void doStuff(int a) and you got an error. I remembered hacking around that one, if anybody knows how to do it better it would be good to know: void doStuff(int a) { // Do stuff. } void templ(T...)() if (T.length==1) { writeln( T.stringof[6..$-1] ); } void main() { templ!doStuff(); }Jacob Carlborg Wrote:What's the big deal? import std.stdio; void doStuff() { } void templ(alias fun)() { writeln(fun.stringof); // prints "main()" } void main() { templ!(main); } Works for both D1 and D2On 10/14/09 06:36, dsimcha wrote:No, the whole point is that I want to print "doStuff".Is there a way to get the name of an alias parameter at compiletime? Forexample: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }Do you want that to print "fun" instead of "doStuff"?
Oct 15 2009
Jacob Carlborg wrote: ...Oh, that problem. Just use a function pointer and get the name of that instead, like this: http://www.dsource.org/projects/dstep/browser/dstep/internal/Traits.d (functionNameOf at line 17)Right, good to know LDC does get it right. I found one bugzilla report about it, though I'm not sure it really is a bug: http://d.puremagic.com/issues/show_bug.cgi?id=1373 There sure are a lot of bugs concerning .stringof!
Oct 15 2009
Bah, I replied too soon. These also work, makes sense now I think about it: void doStuff(int a ) {} void templ(alias fun)() { writeln( (&fun).stringof[2..$] ); // prints doStuff (really) writeln( fun(int.init).stringof ); // prints doStuff(0) }
Oct 15 2009