Do string mixins work under -betterC?

IchorDev zxinsworld at gmail.com
Sun Jan 11 07:06:03 UTC 2026


On Wednesday, 17 December 2025 at 15:12:59 UTC, Kapendev wrote:
> My solution to this is something like:
>
> ```d
> string genStuff(string val)() {
>     return val ~ ";\n";
> }
> ```
>
> This is **in my opinion** the best way to do things if you use 
> the `betterC` flag.

There are two better options in terms of compile speed:

First, if you will be using the exact same parameters in many 
places, you can utilise template caching by assigning the result 
of your function to an enum (so that the function isn't re-run by 
the CTFE engine in every place that you use it):
```d
enum genStuff(string val) = (){
	return val ~ ";\n";
}();

mixin(genStuff("int a"));
void foo(){
	genStuff("int a");
}
void bar(){
	genStuff("int a"));
}
```
Second, if you anticipate that you will use different parameters 
every time, then you don't want to be generating a template for 
every set of parameters because it's somewhat expensive. So we 
can alleviate the template while making the function compile-time 
only by just assigning it to an enum:
```d
enum genStuff = (string val){
	return val ~ ";\n";
};

mixin(genStuff("int a") ~ genStuff("string b") ~ 
genStuff("void[0][size_t] c"));
```


More information about the Digitalmars-d-learn mailing list