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"));
```
  • Re: Do string mi... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
    • Re: Do stri... DLearner via Digitalmars-d-learn
      • Re: Do ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
        • Re:... DLearner via Digitalmars-d-learn
          • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
            • ... DLearner via Digitalmars-d-learn
      • Re: Do ... Adam D. Ruppe via Digitalmars-d-learn
        • Re:... DLearner via Digitalmars-d-learn
      • Re: Do ... abc via Digitalmars-d-learn
  • Re: Do string mi... Kapendev via Digitalmars-d-learn
    • Re: Do stri... IchorDev via Digitalmars-d-learn

Reply via email to