November 14

Right now, I've been implementing classes separately, and I need a dummy symbol. The best world is not even having a symbol but having only its implementation, for example, I would like being able to do that:

void pragma(mangle, "test")(int a){
    writeln(a);
}

Is it possible somehow to do that?

November 14

On Tuesday, 14 November 2023 at 13:43:03 UTC, Hipreme wrote:

>

Right now, I've been implementing classes separately, and I need a dummy symbol. The best world is not even having a symbol but having only its implementation, for example, I would like being able to do that:

void pragma(mangle, "test")(int a){
    writeln(a);
}

Is it possible somehow to do that?

You can use the following string mixin to generate an identifier:

// Mixin to generate a new identifier that won't repeat within a scope
enum gensym(string prefix = "_gensym") =
	`"` ~ prefix ~ `" ~ __traits(identifier, {})["__lambda".length .. $]`;

But since D currently doesn't support mixing in only a function's name, you'd have to put the entire function definition inside a mixin:

mixin(q{void pragma(mangle, "test") }, mixin(gensym), q{(int a) {
    writeln(a);
}});