digitalmars.D.learn - Does exists some way to define a implementation for a symbol?
- Hipreme (10/10) Nov 14 2023 Right now, I've been implementing classes separately, and I need
- Paul Backus (17/27) Nov 14 2023 You can use the following string mixin to generate an identifier:
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: ```d void pragma(mangle, "test")(int a){ writeln(a); } ``` Is it possible somehow to do that?
Nov 14 2023
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: ```d 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: ```d // 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: ```d mixin(q{void pragma(mangle, "test") }, mixin(gensym), q{(int a) { writeln(a); }}); ```
Nov 14 2023