digitalmars.D - Getting symbol of current function?
- Tomer Filiba (18/18) Dec 07 2018 Is there some clever way to get the *symbol* of the current
- Basile B. (4/8) Dec 07 2018 Maybe this would be useful but the way __FUNCTION__ works now
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (6/23) Dec 07 2018 What you're looking for is __traits(parent, {}).
- John Chapman (2/3) Dec 07 2018 This is better than my solution.
- Tomer Filiba (2/3) Dec 11 2018 Thanks Simen, that's awesome!
- John Chapman (13/31) Dec 07 2018 This works:
- Stefan Koch (3/21) Dec 07 2018 It'd be awesome to have symbol variables in general.
- angel (3/28) Dec 07 2018 When you are at it, look also at the "name" of the current lambda
- Atila Neves (3/12) Dec 07 2018 writeln({ return " my name is " ~ __traits(identifier,
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
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
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
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
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
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. -tomerThis 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
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. -tomerIt'd be awesome to have symbol variables in general. I am currently looking into that :)
Dec 07 2018
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:When you are at it, look also at the "name" of the current lambda function, so that it can be called recursively.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. -tomerIt'd be awesome to have symbol variables in general. I am currently looking into that :)
Dec 07 2018
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:writeln({ return " my name is " ~ __traits(identifier, __traits(parent, {})); }());On Friday, 7 December 2018 at 10:51:38 UTC, Tomer Filiba wrote:When you are at it, look also at the "name" of the current lambda function, so that it can be called recursively.[...]It'd be awesome to have symbol variables in general. I am currently looking into that :)
Dec 07 2018