Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
December 07, 2018 Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
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 |
December 07, 2018 Re: Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomer Filiba | 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.
|
December 07, 2018 Re: Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomer Filiba | 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
|
December 07, 2018 Re: Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomer Filiba | 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.
|
December 07, 2018 Re: Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | 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.
|
December 07, 2018 Re: Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomer Filiba | 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 :)
|
December 07, 2018 Re: Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | 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.
|
December 07, 2018 Re: Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to angel | 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, {})); }());
|
December 12, 2018 Re: Getting symbol of current function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | 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!
|
Copyright © 1999-2021 by the D Language Foundation