digitalmars.D - Re: Get name of alias parameter at compile time?
- dsimcha <dsimcha yahoo.com> Oct 15 2009
- "Denis Koroskin" <2korden gmail.com> Oct 15 2009
- Lutger <lutger.blijdestijn gmail.com> Oct 15 2009
- dsimcha <dsimcha jhmi.edu> Oct 15 2009
- Lutger <lutger.blijdestijn gmail.com> Oct 15 2009
- Jacob Carlborg <doob me.com> Oct 15 2009
- Jacob Carlborg <doob me.com> Oct 15 2009
- Lutger <lutger.blijdestijn gmail.com> Oct 15 2009
- Lutger <lutger.blijdestijn gmail.com> Oct 15 2009
Jacob Carlborg Wrote: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"?
No, the whole point is that I want to print "doStuff".
Oct 15 2009
On Thu, 15 Oct 2009 18:22:37 +0400, dsimcha <dsimcha yahoo.com> wrote:Jacob Carlborg Wrote:On 10/14/09 06:36, dsimcha wrote:Is there a way to get the name of an alias parameter at compile
example: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }
Do you want that to print "fun" instead of "doStuff"?
No, the whole point is that I want to print "doStuff".
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 D2
Oct 15 2009
Denis Koroskin wrote:On Thu, 15 Oct 2009 18:22:37 +0400, dsimcha <dsimcha yahoo.com> wrote:Jacob Carlborg Wrote:On 10/14/09 06:36, dsimcha wrote:Is there a way to get the name of an alias parameter at compile
example: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }
Do you want that to print "fun" instead of "doStuff"?
No, the whole point is that I want to print "doStuff".
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 D2
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(); }
Oct 15 2009
== Quote from Lutger (lutger.blijdestijn gmail.com)'s articleDenis Koroskin wrote:On Thu, 15 Oct 2009 18:22:37 +0400, dsimcha <dsimcha yahoo.com> wrote:Jacob Carlborg Wrote:On 10/14/09 06:36, dsimcha wrote:Is there a way to get the name of an alias parameter at compile
example: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }
Do you want that to print "fun" instead of "doStuff"?
No, the whole point is that I want to print "doStuff".
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 D2
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(); }
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.
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: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.
See my reply to dsimcha
Oct 15 2009
On 10/15/09 17:55, dsimcha wrote:== Quote from Lutger (lutger.blijdestijn gmail.com)'s articleDenis Koroskin wrote:On Thu, 15 Oct 2009 18:22:37 +0400, dsimcha<dsimcha yahoo.com> wrote:Jacob Carlborg Wrote:On 10/14/09 06:36, dsimcha wrote:Is there a way to get the name of an alias parameter at compile
example: void doStuff() { // Do stuff. } void templ(alias fun)() { writeln(fun.stringof); // Prints doStuff. }
Do you want that to print "fun" instead of "doStuff"?
No, the whole point is that I want to print "doStuff".
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 D2
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(); }
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.
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)
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