Why is creating of if Expressions not allowed?

Paul Backus snarwin at gmail.com
Sun Mar 24 18:43:51 UTC 2019


On Sunday, 24 March 2019 at 16:18:49 UTC, sighoya wrote:
> Why
>
> auto GenIf()()
> {
>     return mixin("if(true) { return true;} else {return 
> false;}");
> }
>
> public bool testFunction2()
> {
>     GenIf!();
> }
>
>
> gives me:
>
> onlineapp.d-mixin-3(3): Error: expression expected, not if
> onlineapp.d(8): Error: template instance `onlineapp.GenIf!()` 
> error instantiating

You can't return the result of a string mixin from a function. 
Instead, return a string from your `GenIf` function, and mix it 
in at the call site:

string GenIf()
{
     return "if (true) { return true; } else { return false; }";
}

bool testFunction()
{
     mixin(GenIf());
}


More information about the Digitalmars-d-learn mailing list