www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Trying to get current function name results in compiler error with

reply Arun Chandrasekaran <aruncxy gmail.com> writes:
I'm trying to get the current function name and apparently the 
commented line errors out.

What am I doing wrong?

https://run.dlang.io/is/EGsRU2

```


void main()
{
     import std.experimental.all;
     void foo() {
         // __traits(identifier, mixin(__FUNCTION__)).writeln; // 
compilation error
         __FUNCTION__.split('.')[$-1].writeln;
     }
     __traits(identifier, mixin(__FUNCTION__)).writeln;
     __FUNCTION__.split('.')[$-1].writeln;
     foo();
}
```
Dec 06 2018
next sibling parent Neia Neutuladh <neia ikeran.org> writes:
On Fri, 07 Dec 2018 02:37:34 +0000, Arun Chandrasekaran wrote:
 I'm trying to get the current function name and apparently the commented
 line errors out.
 
 What am I doing wrong?
Referring to nested functions is weird. Dotted identifiers let you traverse aggregates. Modules, C++ namespaces (ugh), enums, structs, identifiers, unions, that sort of thing. They *don't* let you traverse functions to refer to symbols defined inside those functions. *Separately*, nested functions have names that look like dotted identifiers. But you can't use that to refer to them, because that would make it *very* awkward to do symbol lookup. For example: struct Foo { int bar; } Foo test() { void bar() { } writeln(&test.bar); return Foo(); } Should the `writeln` line invoke the `test` function, get the `bar` field from its result, and take its address? Or should it take the address of the nested function `bar`?
Dec 06 2018
prev sibling parent Sepheus <noreply sepheus.co.uk> writes:
On Friday, 7 December 2018 at 02:37:34 UTC, Arun Chandrasekaran 
wrote:
 I'm trying to get the current function name and apparently the 
 commented line errors out.

 What am I doing wrong?

 https://run.dlang.io/is/EGsRU2

 ```


 void main()
 {
     import std.experimental.all;
     void foo() {
         // __traits(identifier, mixin(__FUNCTION__)).writeln; 
 // compilation error
         __FUNCTION__.split('.')[$-1].writeln;
     }
     __traits(identifier, mixin(__FUNCTION__)).writeln;
     __FUNCTION__.split('.')[$-1].writeln;
     foo();
 }
 ```
Looks like this will do the trick https://run.dlang.io/is/cX3S37
Dec 07 2018