Does exists some way to define a implementation for a symbol?

Paul Backus snarwin at gmail.com
Tue Nov 14 14:08:21 UTC 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);
}});
```


More information about the Digitalmars-d-learn mailing list