Is there a way to convert a function variable to a string in D?

Is there any way, given the function variable, to get the function name as a string? For example, if I have:

void function(int) func; 

Is there any function x () such that I could get:

 x(func) == "func"; 

? I feel this is possible with mixins, but I don't understand how to implement this.

+6
source share
2 answers
 func.stringof 

- This is what you need.

You can also make a template:

 template Name(alias Func) { enum Name = Func.stringof; } void func() { } pragma(msg, Name!(func)); //prints func() 
+7
source

The simplest solution that comes to my mind:

Your name can be stored in a string, and mixin 'ed if necessary, something like:

 string func_name = "func"; ... int param = 294; mixin(func_name ~ "(" ~ to!string(param) ~ ")"); 
0
source

Source: https://habr.com/ru/post/890987/


All Articles