mixin under -betterC

Paul Backus snarwin at gmail.com
Thu Nov 23 17:02:58 UTC 2023


On Thursday, 23 November 2023 at 16:33:52 UTC, DLearner wrote:
> Code below is intended to test simple mixin with lambda 
> function under -betterC.
> Works with full-D, but fails with 'needs GC' errors under 
> -betterC.
>
> Why is this so, bearing in mind the concatenations are executed 
> at
> compile, not run, time?

This is a known limitation: 
https://issues.dlang.org/show_bug.cgi?id=23637

The easiest way to work around it is to change `mxnTest` from a 
function to a templated manifest constant:

```d
enum mxnTest(string strVar1, string strVar2) =
     `(int Var1, int Var2) {
         if (Var1 > Var2) {
            return true;
         } else {
            return false;
         }
     }(` ~ strVar1 ~ `,` ~ strVar2 ~ `)`;
```

Keep in mind that you will also have to change the call site to 
pass `"Var_A"` and `"Var_B"` as template arguments:

```d
     // Note the ! in front of the argument list
     FirstVarGreater = mixin(mxnTest!("Var_A", "Var_B"));
```


More information about the Digitalmars-d-learn mailing list