Thread overview
Is there a way to get the name of the current function?
Mar 07, 2023
rempas
Mar 07, 2023
JG
Mar 08, 2023
rempas
Mar 07, 2023
ryuukk_
Mar 08, 2023
rempas
March 07, 2023

For example, in the given code:

void my_function() {
  import std.stdio;

  writeln("The name of the function is: ", <fn_name_macro_or_something>);
}

Is there something to put in the place of <fn_name_macro_or_something> to get the name of the function?

March 07, 2023

On Tuesday, 7 March 2023 at 22:11:49 UTC, rempas wrote:

>

For example, in the given code:

void my_function() {
  import std.stdio;

  writeln("The name of the function is: ", <fn_name_macro_or_something>);
}

Is there something to put in the place of <fn_name_macro_or_something> to get the name of the function?

Yes, see here: https://dlang.org/spec/expression.html#specialkeywords

March 07, 2023

On Tuesday, 7 March 2023 at 22:11:49 UTC, rempas wrote:

>

For example, in the given code:

void my_function() {
  import std.stdio;

  writeln("The name of the function is: ", <fn_name_macro_or_something>);
}

Is there something to put in the place of <fn_name_macro_or_something> to get the name of the function?

import std.stdio;
void main()
{
    writeln("file:        ", __FILE__);
    writeln("function is: ", __FUNCTION__);
    writeln("function is: ", __PRETTY_FUNCTION__ );
}

$ dmd -run tester.d
file:        tester.d
function is: tester.main
function is: void tester.main()

You can even get the calling function name:

import std.stdio;
void main()
{
    log("hello");
}


void log(string msg, string file = __FILE__, int line = __LINE__, string fn = __PRETTY_FUNCTION__)
{
    writeln(file,":",line,"|", fn,"|> ", msg);
}

https://dlang.org/spec/expression.html#specialkeywords

March 08, 2023

On Tuesday, 7 March 2023 at 22:19:22 UTC, JG wrote:

>

Yes, see here: https://dlang.org/spec/expression.html#specialkeywords

OMG, so that's when they are located! I was trying to search the spec and find them in different pages but had no luck! Thanks a lot for the help, have a great day!

March 08, 2023

On Tuesday, 7 March 2023 at 22:28:41 UTC, ryuukk_ wrote:

>
import std.stdio;
void main()
{
    writeln("file:        ", __FILE__);
    writeln("function is: ", __FUNCTION__);
    writeln("function is: ", __PRETTY_FUNCTION__ );
}

$ dmd -run tester.d
file:        tester.d
function is: tester.main
function is: void tester.main()

You can even get the calling function name:

import std.stdio;
void main()
{
    log("hello");
}


void log(string msg, string file = __FILE__, int line = __LINE__, string fn = __PRETTY_FUNCTION__)
{
    writeln(file,":",line,"|", fn,"|> ", msg);
}

https://dlang.org/spec/expression.html#specialkeywords

Thanks a lot for the detailed examples! Have an amazing day!