Is there a way to get the name of the current function?
ryuukk_
ryuukk.dev at gmail.com
Tue Mar 7 22:28:41 UTC 2023
On Tuesday, 7 March 2023 at 22:11:49 UTC, rempas wrote:
> For example, in the given code:
>
> ```d
> 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
More information about the Digitalmars-d-learn
mailing list