Mark LeMoine
Mark LeMoine

Reputation: 4628

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

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

void function(int) func;

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

x(func) == "func";

? I feel like this would be possible using mixins, but I'm confused as to how to implement this.

Upvotes: 6

Views: 109

Answers (2)

vines
vines

Reputation: 5225

Simplest solution that comes to my mind:

You can have its name stored in a string, and mixin'ed where necessary, something like:

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

Upvotes: 0

user541686
user541686

Reputation: 210525

func.stringof

is what you need.

You could also make a template:

template Name(alias Func) { enum Name = Func.stringof; }

void func() { }
pragma(msg, Name!(func));    //prints func()

Upvotes: 7

Related Questions