www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Function name as text

reply "Craig Black" <cblack ara.com> writes:
I have been considering porting some C++ code to D.  One of the classes I 
would have to port is an event queue class where each event on the queue has 
a delegate and a text string that points to the function name that the 
delegate refers to.  The function name is used to visualize the event queue 
for run-time debugging purposes.  It is important to capture both the class 
name and the function name as text.


capture the name of the function.  The good (and bad) thing about C++ in 
this case is that when specifying a pointer to a member, you must fully 
qualify the function name, so you would have something like this.

class Foo {
public:
  void bar() {}
};

Foo *foo = new Foo;
Event event = DISPATCH(foo, &Foo::bar);

Using the stringize operator, the DISPATCH macro could capture the text 
string "Foo::bar" as well as the member function pointer.  Here is the 
equivalent code in D..

Foo foo = new Foo;
Event event = dispatch(&foo.bar);

Which is much more elegant, except that I can't figure out a way to capture 
the name of the function and it's class.  I tried fiddling with the stringof 
operator but that doesn't seem to work.

Any ideas? 
Dec 05 2007
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Craig Black" <cblack ara.com> wrote in message 
news:fj714p$2u7b$1 digitalmars.com...
I have been considering porting some C++ code to D.  One of the classes I 
would have to port is an event queue class where each event on the queue 
has a delegate and a text string that points to the function name that the 
delegate refers to.  The function name is used to visualize the event queue 
for run-time debugging purposes.  It is important to capture both the class 
name and the function name as text.


 capture the name of the function.  The good (and bad) thing about C++ in 
 this case is that when specifying a pointer to a member, you must fully 
 qualify the function name, so you would have something like this.

 class Foo {
 public:
  void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the text 
 string "Foo::bar" as well as the member function pointer.  Here is the 
 equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling with 
 the stringof operator but that doesn't seem to work.

 Any ideas?
template dispatch(char[] methodName) { Event dispatch(T)(T thing) { Event ret; ret.name = T.stringof ~ "." ~ methodName; ret.func = mixin("&thing." ~ methodName); return ret; } } Now you can use it like: Event event = dispatch!("bar")(foo); It's kind of backwards-looking, but it's probably the best you can do without macros/static params.
Dec 05 2007
parent reply "Craig Black" <cblack ara.com> writes:
Thanks for the idea.  I came up with something similar.  (See my post after 
yours.)  Again, it would be nice if I didn't have to wrap the name of the 
instance in a string. 
Dec 05 2007
parent "Craig Black" <cblack ara.com> writes:
"Craig Black" <cblack ara.com> wrote in message 
news:fj7683$7n9$1 digitalmars.com...
 Thanks for the idea.  I came up with something similar.  (See my post 
 after yours.)  Again, it would be nice if I didn't have to wrap the name 
 of the instance in a string.
Err, I mean wrap the name of the method in a string.
Dec 05 2007
prev sibling next sibling parent reply "Craig Black" <cblack ara.com> writes:
OK I think it's possible with mixins but the syntax is ugly.  It would be

Event event = mixin(dispatch("&foo.bar"));

Which leads me to an idea that has probably been proposed before.  It would 
be nice if I could shorten this to simply

Event event = dispatch(&foo.bar);

There may be reasons why this syntax would be difficult to achieve, but I 
think it would be worthwhile if we could.

Thoughts?

-Craig
Dec 05 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Craig Black wrote:
 OK I think it's possible with mixins but the syntax is ugly.  It would be
 
 Event event = mixin(dispatch("&foo.bar"));
 
 Which leads me to an idea that has probably been proposed before.  It would 
 be nice if I could shorten this to simply
 
 Event event = dispatch(&foo.bar);
 
 There may be reasons why this syntax would be difficult to achieve, but I 
 think it would be worthwhile if we could.
 
 Thoughts?
My understanding is that that is exactly what macros are going to do. Define dispatch as a macro, then its arguments get passed as strings. I think the main issue is just working out the grammar and all the various compiler bits to make it work. --bb
Dec 05 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fj76f5$7ou$2 digitalmars.com...
 Craig Black wrote:
 OK I think it's possible with mixins but the syntax is ugly.  It would be

 Event event = mixin(dispatch("&foo.bar"));

 Which leads me to an idea that has probably been proposed before.  It 
 would be nice if I could shorten this to simply

 Event event = dispatch(&foo.bar);

 There may be reasons why this syntax would be difficult to achieve, but I 
 think it would be worthwhile if we could.

 Thoughts?
My understanding is that that is exactly what macros are going to do. Define dispatch as a macro, then its arguments get passed as strings. I think the main issue is just working out the grammar and all the various compiler bits to make it work. --bb
Ooooh. macro dispatch(&x.y) { Event(y.stringof, &x.y) } Or something like that. Then Event e = dispatch(&foo.bar); turns into Event e = Event("Foo.bar", &foo.bar); Heck, you could remove the requirement for the ugly ampersand. macro dispatch(x.y) { Event(y.stringof, &x.y) }
Dec 05 2007
parent "Craig Black" <craigblack2 cox.net> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:fj79a1$d2l$1 digitalmars.com...
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fj76f5$7ou$2 digitalmars.com...
 Craig Black wrote:
 OK I think it's possible with mixins but the syntax is ugly.  It would 
 be

 Event event = mixin(dispatch("&foo.bar"));

 Which leads me to an idea that has probably been proposed before.  It 
 would be nice if I could shorten this to simply

 Event event = dispatch(&foo.bar);

 There may be reasons why this syntax would be difficult to achieve, but 
 I think it would be worthwhile if we could.

 Thoughts?
My understanding is that that is exactly what macros are going to do. Define dispatch as a macro, then its arguments get passed as strings. I think the main issue is just working out the grammar and all the various compiler bits to make it work. --bb
Ooooh. macro dispatch(&x.y) { Event(y.stringof, &x.y) } Or something like that. Then Event e = dispatch(&foo.bar); turns into Event e = Event("Foo.bar", &foo.bar); Heck, you could remove the requirement for the ugly ampersand. macro dispatch(x.y) { Event(y.stringof, &x.y) }
It would be very cool if macros worked like this. I think it would actually have to be macro dispatch(&x.y) { Event(typeof(x).stringof ~ "." ~ y.stringof, &x.y) }
Dec 07 2007
prev sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the classes I 
 would have to port is an event queue class where each event on the queue has 
 a delegate and a text string that points to the function name that the 
 delegate refers to.  The function name is used to visualize the event queue 
 for run-time debugging purposes.  It is important to capture both the class 
 name and the function name as text.
 

 capture the name of the function.  The good (and bad) thing about C++ in 
 this case is that when specifying a pointer to a member, you must fully 
 qualify the function name, so you would have something like this.
 
 class Foo {
 public:
   void bar() {}
 };
 
 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);
 
 Using the stringize operator, the DISPATCH macro could capture the text 
 string "Foo::bar" as well as the member function pointer.  Here is the 
 equivalent code in D..
 
 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);
 
 Which is much more elegant, except that I can't figure out a way to capture 
 the name of the function and it's class.  I tried fiddling with the stringof 
 operator but that doesn't seem to work.
 
 Any ideas? 
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
Dec 05 2007
parent reply "Craig Black" <cblack ara.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fj769h$7ou$1 digitalmars.com...
 Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the classes I 
 would have to port is an event queue class where each event on the queue 
 has a delegate and a text string that points to the function name that 
 the delegate refers to.  The function name is used to visualize the event 
 queue for run-time debugging purposes.  It is important to capture both 
 the class name and the function name as text.


 to capture the name of the function.  The good (and bad) thing about C++ 
 in this case is that when specifying a pointer to a member, you must 
 fully qualify the function name, so you would have something like this.

 class Foo {
 public:
   void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the text 
 string "Foo::bar" as well as the member function pointer.  Here is the 
 equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling with 
 the stringof operator but that doesn't seem to work.

 Any ideas?
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
I guess that's not so bad. It would be. Foo *foo = new Foo; Event event = dispatch(&foo.bar, "Foo.bar"); That is probably easier on the eyes than the mixin syntax. It stilll would be cool if the compiler could somehow build the name automatically. Thanks for the help.
Dec 05 2007
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Craig Black wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fj769h$7ou$1 digitalmars.com...
 Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the classes I 
 would have to port is an event queue class where each event on the queue 
 has a delegate and a text string that points to the function name that 
 the delegate refers to.  The function name is used to visualize the event 
 queue for run-time debugging purposes.  It is important to capture both 
 the class name and the function name as text.


 to capture the name of the function.  The good (and bad) thing about C++ 
 in this case is that when specifying a pointer to a member, you must 
 fully qualify the function name, so you would have something like this.

 class Foo {
 public:
   void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the text 
 string "Foo::bar" as well as the member function pointer.  Here is the 
 equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling with 
 the stringof operator but that doesn't seem to work.

 Any ideas?
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
I guess that's not so bad. It would be. Foo *foo = new Foo; Event event = dispatch(&foo.bar, "Foo.bar"); That is probably easier on the eyes than the mixin syntax. It stilll would be cool if the compiler could somehow build the name automatically. Thanks for the help.
Or you could just run the C preprocessor on your source code. Mwahahaha... --bb
Dec 05 2007
prev sibling parent reply Don Clugston <dac nospam.com.au> writes:
Craig Black wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fj769h$7ou$1 digitalmars.com...
 Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the classes I 
 would have to port is an event queue class where each event on the queue 
 has a delegate and a text string that points to the function name that 
 the delegate refers to.  The function name is used to visualize the event 
 queue for run-time debugging purposes.  It is important to capture both 
 the class name and the function name as text.


 to capture the name of the function.  The good (and bad) thing about C++ 
 in this case is that when specifying a pointer to a member, you must 
 fully qualify the function name, so you would have something like this.

 class Foo {
 public:
   void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the text 
 string "Foo::bar" as well as the member function pointer.  Here is the 
 equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling with 
 the stringof operator but that doesn't seem to work.

 Any ideas?
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
I guess that's not so bad. It would be. Foo *foo = new Foo; Event event = dispatch(&foo.bar, "Foo.bar"); That is probably easier on the eyes than the mixin syntax. It stilll would be cool if the compiler could somehow build the name automatically.
Or you could use an alias template parameter, to give the syntax: Event event = dispatch!(foo.bar);
Dec 06 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Don Clugston wrote:
 Craig Black wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fj769h$7ou$1 digitalmars.com...
 Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the 
 classes I would have to port is an event queue class where each 
 event on the queue has a delegate and a text string that points to 
 the function name that the delegate refers to.  The function name is 
 used to visualize the event queue for run-time debugging purposes.  
 It is important to capture both the class name and the function name 
 as text.

 In C++ I had a macro called DISPATCH that used the stringize 

 thing about C++ in this case is that when specifying a pointer to a 
 member, you must fully qualify the function name, so you would have 
 something like this.

 class Foo {
 public:
   void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the 
 text string "Foo::bar" as well as the member function pointer.  Here 
 is the equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling 
 with the stringof operator but that doesn't seem to work.

 Any ideas?
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
I guess that's not so bad. It would be. Foo *foo = new Foo; Event event = dispatch(&foo.bar, "Foo.bar"); That is probably easier on the eyes than the mixin syntax. It stilll would be cool if the compiler could somehow build the name automatically.
Or you could use an alias template parameter, to give the syntax: Event event = dispatch!(foo.bar);
Really? Can you take the stringof an alias parameter and get back "foo.bar" ? If so then nifty! --bb
Dec 06 2007
next sibling parent reply Don Clugston <dac nospam.com.au> writes:
Bill Baxter wrote:
 Don Clugston wrote:
 Craig Black wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fj769h$7ou$1 digitalmars.com...
 Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the 
 classes I would have to port is an event queue class where each 
 event on the queue has a delegate and a text string that points to 
 the function name that the delegate refers to.  The function name 
 is used to visualize the event queue for run-time debugging 
 purposes.  It is important to capture both the class name and the 
 function name as text.

 In C++ I had a macro called DISPATCH that used the stringize 

 thing about C++ in this case is that when specifying a pointer to a 
 member, you must fully qualify the function name, so you would have 
 something like this.

 class Foo {
 public:
   void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the 
 text string "Foo::bar" as well as the member function pointer.  
 Here is the equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling 
 with the stringof operator but that doesn't seem to work.

 Any ideas?
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
I guess that's not so bad. It would be. Foo *foo = new Foo; Event event = dispatch(&foo.bar, "Foo.bar"); That is probably easier on the eyes than the mixin syntax. It stilll would be cool if the compiler could somehow build the name automatically.
Or you could use an alias template parameter, to give the syntax: Event event = dispatch!(foo.bar);
Really? Can you take the stringof an alias parameter and get back "foo.bar" ? If so then nifty! --bb
Unfortunately it's not quite so simply, since there are so many bugs in .stringof. I'm unable to get the 'foo'. ---- class Foo { void bar(int d) {} } void GetName(alias F)() { pragma(msg, (&F).stringof); // prints "& bar" } void main() { GetName!(Foo.bar); } ---- I have however done this previously using .mangleof to retrieve the fully qualified name. The behaviour with alias template parameters was one of the parts of my NameOf module which didn't get into .stringof. Sure would be nice if F.stringof worked.
Dec 06 2007
parent reply "Craig Black" <cblack ara.com> writes:
"Don Clugston" <dac nospam.com.au> wrote in message 
news:fj8j1p$2per$1 digitalmars.com...
 Bill Baxter wrote:
 Don Clugston wrote:
 Craig Black wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fj769h$7ou$1 digitalmars.com...
 Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the 
 classes I would have to port is an event queue class where each event 
 on the queue has a delegate and a text string that points to the 
 function name that the delegate refers to.  The function name is used 
 to visualize the event queue for run-time debugging purposes.  It is 
 important to capture both the class name and the function name as 
 text.

 In C++ I had a macro called DISPATCH that used the stringize operator 

 about C++ in this case is that when specifying a pointer to a member, 
 you must fully qualify the function name, so you would have something 
 like this.

 class Foo {
 public:
   void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the 
 text string "Foo::bar" as well as the member function pointer.  Here 
 is the equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling 
 with the stringof operator but that doesn't seem to work.

 Any ideas?
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
I guess that's not so bad. It would be. Foo *foo = new Foo; Event event = dispatch(&foo.bar, "Foo.bar"); That is probably easier on the eyes than the mixin syntax. It stilll would be cool if the compiler could somehow build the name automatically.
Or you could use an alias template parameter, to give the syntax: Event event = dispatch!(foo.bar);
Really? Can you take the stringof an alias parameter and get back "foo.bar" ? If so then nifty! --bb
Unfortunately it's not quite so simply, since there are so many bugs in .stringof. I'm unable to get the 'foo'. ---- class Foo { void bar(int d) {} } void GetName(alias F)() { pragma(msg, (&F).stringof); // prints "& bar" } void main() { GetName!(Foo.bar); } ---- I have however done this previously using .mangleof to retrieve the fully qualified name. The behaviour with alias template parameters was one of the parts of my NameOf module which didn't get into .stringof. Sure would be nice if F.stringof worked.
I ran into this same thing with stringof. Are you sure that this is a bug? Has it been officially reported?
Dec 06 2007
parent Don Clugston <dac nospam.com.au> writes:
Craig Black wrote:
 "Don Clugston" <dac nospam.com.au> wrote in message 
 news:fj8j1p$2per$1 digitalmars.com...
 Bill Baxter wrote:
 Don Clugston wrote:
 Craig Black wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fj769h$7ou$1 digitalmars.com...
 Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the 
 classes I would have to port is an event queue class where each event 
 on the queue has a delegate and a text string that points to the 
 function name that the delegate refers to.  The function name is used 
 to visualize the event queue for run-time debugging purposes.  It is 
 important to capture both the class name and the function name as 
 text.

 In C++ I had a macro called DISPATCH that used the stringize operator 

 about C++ in this case is that when specifying a pointer to a member, 
 you must fully qualify the function name, so you would have something 
 like this.

 class Foo {
 public:
   void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the 
 text string "Foo::bar" as well as the member function pointer.  Here 
 is the equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling 
 with the stringof operator but that doesn't seem to work.

 Any ideas?
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
I guess that's not so bad. It would be. Foo *foo = new Foo; Event event = dispatch(&foo.bar, "Foo.bar"); That is probably easier on the eyes than the mixin syntax. It stilll would be cool if the compiler could somehow build the name automatically.
Or you could use an alias template parameter, to give the syntax: Event event = dispatch!(foo.bar);
Really? Can you take the stringof an alias parameter and get back "foo.bar" ? If so then nifty! --bb
Unfortunately it's not quite so simply, since there are so many bugs in .stringof. I'm unable to get the 'foo'. ---- class Foo { void bar(int d) {} } void GetName(alias F)() { pragma(msg, (&F).stringof); // prints "& bar" } void main() { GetName!(Foo.bar); } ---- I have however done this previously using .mangleof to retrieve the fully qualified name. The behaviour with alias template parameters was one of the parts of my NameOf module which didn't get into .stringof. Sure would be nice if F.stringof worked.
I ran into this same thing with stringof. Are you sure that this is a bug?
It's hard to say. stringof was originally a 'hidden feature'/easter egg that didn't get mentioned in the changelog. It's in the docs, but it behaves quite differently from documented, except in simple cases.
 Has it been officially reported? 
Not that I know of. Even most of the examples in the docs don't work as advertised, so there hasn't been much motivation to report these secondary cases.
Dec 06 2007
prev sibling parent "Craig Black" <cblack ara.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fj8dlo$2fuj$2 digitalmars.com...
 Don Clugston wrote:
 Craig Black wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fj769h$7ou$1 digitalmars.com...
 Craig Black wrote:
 I have been considering porting some C++ code to D.  One of the 
 classes I would have to port is an event queue class where each event 
 on the queue has a delegate and a text string that points to the 
 function name that the delegate refers to.  The function name is used 
 to visualize the event queue for run-time debugging purposes.  It is 
 important to capture both the class name and the function name as 
 text.

 In C++ I had a macro called DISPATCH that used the stringize operator 

 C++ in this case is that when specifying a pointer to a member, you 
 must fully qualify the function name, so you would have something like 
 this.

 class Foo {
 public:
   void bar() {}
 };

 Foo *foo = new Foo;
 Event event = DISPATCH(foo, &Foo::bar);

 Using the stringize operator, the DISPATCH macro could capture the 
 text string "Foo::bar" as well as the member function pointer.  Here 
 is the equivalent code in D..

 Foo foo = new Foo;
 Event event = dispatch(&foo.bar);

 Which is much more elegant, except that I can't figure out a way to 
 capture the name of the function and it's class.  I tried fiddling 
 with the stringof operator but that doesn't seem to work.

 Any ideas?
There probably isn't a way to do it right now without using a string mixin, which uglies things up on the calling side: Event event = mixin(dispatch("&foo.bar")); Macros are supposed to give us a way to clean that up. But for now you're probably better off just passing the name separately like dispatch(&foo.bar, "foo"); --bb
I guess that's not so bad. It would be. Foo *foo = new Foo; Event event = dispatch(&foo.bar, "Foo.bar"); That is probably easier on the eyes than the mixin syntax. It stilll would be cool if the compiler could somehow build the name automatically.
Or you could use an alias template parameter, to give the syntax: Event event = dispatch!(foo.bar);
Really? Can you take the stringof an alias parameter and get back "foo.bar" ? If so then nifty! --bb
Sorry to be picky, but getting back the string "foo.bar" is not sufficient. I need the name of the class, not the instance name.
Dec 06 2007
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Don Clugston" <dac nospam.com.au> wrote in message 
news:fj8ap7$29k9$1 digitalmars.com...
 Or you could use an alias template parameter, to give the syntax:

 Event event = dispatch!(foo.bar);
Unfortunately not, since passing foo.bar to an alias parameter will get you an alias to the method bar instead of the entire expression. :\
Dec 06 2007