mixin under -betterC

DLearner bmqazwsx123 at gmail.com
Sat Dec 9 23:39:49 UTC 2023


On Sunday, 26 November 2023 at 15:35:39 UTC, Adam D Ruppe wrote:
> On Thursday, 23 November 2023 at 16:33:52 UTC, DLearner wrote:
>> string mxnTest(string strVar1, string strVar2) {
>>    return `(int Var1, int Var2) {
>>       if (Var1 > Var2) {
>>          return true;
>>       } else {
>>          return false;
>>       }
>>    }(` ~ strVar1 ~ `,` ~ strVar2 ~ `)`;
>> }
>> ```
>
> This function exists at runtime. Another module could, in 
> theory, import it and call it. A shared library could, in 
> theory, export it. You used it at compile time, but the 
> function is available for other users too.
>
> betterC doesn't know the difference between theory and practice.

 From your comments and others on this thread:

```
// Test harness

    extern(C) void main() {
       import core.stdc.stdio : printf;
       import testmod;

       bool FirstVarGreater;
       int Var_A = 6;
       int Var_B = 5;


       FirstVarGreater = mixin(mxnTest("Var_A", "Var_B"));
       if (FirstVarGreater) {
             printf("First Var is Greater\n");
       } else {
             printf("First Var is not Greater\n");
       }
    }

// testmod

string mxnTest(string strVar1, string strVar2) {

    if (__ctfe) {
       return `(int Var1, int Var2) {
          if (Var1 > Var2) {
             return true;
          } else {
             return false;
          }
       }(` ~ strVar1 ~ `,` ~ strVar2 ~ `)`;
    } else {
       return ``;
    }
}
```

Works, avoid templates + -betterC compliant, but to me clumsy.



More information about the Digitalmars-d-learn mailing list