www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Getting symbol of current function?

reply Tomer Filiba <tomerfiliba gmail.com> writes:
Is there some clever way to get the *symbol* of the current 
function, given overloads? I want to log the current function's 
arguments, but __FUNCTION__ et al only give me a name, which 
resolves to the first overload. For instance,

     void f(int x) {
         pragma(msg, "overload1", 
Parameters!(mixin(__FUNCTION__)));
     }
     void f(int x, int y) {
         pragma(msg, "overload2", 
Parameters!(mixin(__FUNCTION__)));
     }

shows
     overload1(int)
     overload2(int)

It would be awesome to have something like __FUNCTION_SYM__ or 
so, which references the actual symbol.

-tomer
Dec 07 2018
next sibling parent Basile B. <b2.temp gmx.com> writes:
On Friday, 7 December 2018 at 10:51:38 UTC, Tomer Filiba wrote:
 Is there some clever way to get the *symbol* of the current 
 function, given overloads? I want to log the current function's 
 arguments, but __FUNCTION__ et al only give me a name, which 
 resolves to the first overload.
Maybe this would be useful but the way __FUNCTION__ works now looks like a bug. It should give the string representation of the one we're in.
Dec 07 2018
prev sibling next sibling parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Friday, 7 December 2018 at 10:51:38 UTC, Tomer Filiba wrote:
 Is there some clever way to get the *symbol* of the current 
 function, given overloads? I want to log the current function's 
 arguments, but __FUNCTION__ et al only give me a name, which 
 resolves to the first overload. For instance,

     void f(int x) {
         pragma(msg, "overload1", 
 Parameters!(mixin(__FUNCTION__)));
     }
     void f(int x, int y) {
         pragma(msg, "overload2", 
 Parameters!(mixin(__FUNCTION__)));
     }

 shows
     overload1(int)
     overload2(int)

 It would be awesome to have something like __FUNCTION_SYM__ or 
 so, which references the actual symbol.
What you're looking for is __traits(parent, {}). mixin(__FUNCTION__) gives you the overload set by that name, not just a single function. -- Simen
Dec 07 2018
next sibling parent John Chapman <johnch_atms hotmail.com> writes:
On Friday, 7 December 2018 at 11:16:20 UTC, Simen Kjærås wrote:
 What you're looking for is __traits(parent, {}).
This is better than my solution.
Dec 07 2018
prev sibling parent Tomer Filiba <tomerfiliba gmail.com> writes:
On Friday, 7 December 2018 at 11:16:20 UTC, Simen Kjærås wrote:
 What you're looking for is __traits(parent, {}).
Thanks Simen, that's awesome!
Dec 11 2018
prev sibling next sibling parent John Chapman <johnch_atms hotmail.com> writes:
On Friday, 7 December 2018 at 10:51:38 UTC, Tomer Filiba wrote:
 Is there some clever way to get the *symbol* of the current 
 function, given overloads? I want to log the current function's 
 arguments, but __FUNCTION__ et al only give me a name, which 
 resolves to the first overload. For instance,

     void f(int x) {
         pragma(msg, "overload1", 
 Parameters!(mixin(__FUNCTION__)));
     }
     void f(int x, int y) {
         pragma(msg, "overload2", 
 Parameters!(mixin(__FUNCTION__)));
     }

 shows
     overload1(int)
     overload2(int)

 It would be awesome to have something like __FUNCTION_SYM__ or 
 so, which references the actual symbol.

 -tomer
This works: void f(int x) { pragma(msg, "overload1", Parameters!(__traits(parent, x))); // prints "overload1(int)" } void f(int x, int y) { pragma(msg, "overload2", Parameters!(__traits(parent, x))); // prints "overload2(int, int)" } If you don't want to refer to the parameters, you can add a dummy variable inside each of the functions and use __traits(parent, dummy) instead.
Dec 07 2018
prev sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Friday, 7 December 2018 at 10:51:38 UTC, Tomer Filiba wrote:
 Is there some clever way to get the *symbol* of the current 
 function, given overloads? I want to log the current function's 
 arguments, but __FUNCTION__ et al only give me a name, which 
 resolves to the first overload. For instance,

     void f(int x) {
         pragma(msg, "overload1", 
 Parameters!(mixin(__FUNCTION__)));
     }
     void f(int x, int y) {
         pragma(msg, "overload2", 
 Parameters!(mixin(__FUNCTION__)));
     }

 shows
     overload1(int)
     overload2(int)

 It would be awesome to have something like __FUNCTION_SYM__ or 
 so, which references the actual symbol.

 -tomer
It'd be awesome to have symbol variables in general. I am currently looking into that :)
Dec 07 2018
parent reply angel <andrey.gelman gmail.com> writes:
On Friday, 7 December 2018 at 14:56:22 UTC, Stefan Koch wrote:
 On Friday, 7 December 2018 at 10:51:38 UTC, Tomer Filiba wrote:
 Is there some clever way to get the *symbol* of the current 
 function, given overloads? I want to log the current 
 function's arguments, but __FUNCTION__ et al only give me a 
 name, which resolves to the first overload. For instance,

     void f(int x) {
         pragma(msg, "overload1", 
 Parameters!(mixin(__FUNCTION__)));
     }
     void f(int x, int y) {
         pragma(msg, "overload2", 
 Parameters!(mixin(__FUNCTION__)));
     }

 shows
     overload1(int)
     overload2(int)

 It would be awesome to have something like __FUNCTION_SYM__ or 
 so, which references the actual symbol.

 -tomer
It'd be awesome to have symbol variables in general. I am currently looking into that :)
When you are at it, look also at the "name" of the current lambda function, so that it can be called recursively.
Dec 07 2018
parent Atila Neves <atila.neves gmail.com> writes:
On Friday, 7 December 2018 at 16:45:00 UTC, angel wrote:
 On Friday, 7 December 2018 at 14:56:22 UTC, Stefan Koch wrote:
 On Friday, 7 December 2018 at 10:51:38 UTC, Tomer Filiba wrote:
 [...]
It'd be awesome to have symbol variables in general. I am currently looking into that :)
When you are at it, look also at the "name" of the current lambda function, so that it can be called recursively.
writeln({ return " my name is " ~ __traits(identifier, __traits(parent, {})); }());
Dec 07 2018