Thread overview
Trying to get current function name results in compiler error with __traits
Dec 07, 2018
Neia Neutuladh
Dec 07, 2018
Sepheus
December 07, 2018
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

```
#!/usr/bin/rdmd

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();
}
```

December 07, 2018
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`?
December 07, 2018
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
>
> ```
> #!/usr/bin/rdmd
>
> 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