www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Get name of alias parameter at compile time?

reply dsimcha <dsimcha yahoo.com> writes:
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
next sibling parent Jacob Carlborg <doob me.com> writes:
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
prev sibling parent reply dsimcha <dsimcha yahoo.com> writes:
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
parent reply "Denis Koroskin" <2korden gmail.com> writes:
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  
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".
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
parent reply Lutger <lutger.blijdestijn gmail.com> writes:
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
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".
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
next sibling parent reply dsimcha <dsimcha jhmi.edu> writes:
== Quote from Lutger (lutger.blijdestijn gmail.com)'s article
 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
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".
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(); }
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
next sibling parent reply Lutger <lutger.blijdestijn gmail.com> writes:
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
parent Jacob Carlborg <doob me.com> writes:
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
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 10/15/09 17:55, dsimcha wrote:
 == Quote from Lutger (lutger.blijdestijn gmail.com)'s article
 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
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".
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(); }
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
parent Lutger <lutger.blijdestijn gmail.com> writes:
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
prev sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
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