www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to get the name of the current function?

reply rempas <rempas tutanota.com> writes:
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?
Mar 07 2023
next sibling parent reply JG <someone simewhere.com> writes:
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?
Yes, see here: https://dlang.org/spec/expression.html#specialkeywords
Mar 07 2023
parent rempas <rempas tutanota.com> writes:
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!
Mar 07 2023
prev sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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
Mar 07 2023
parent rempas <rempas tutanota.com> writes:
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!
Mar 07 2023