Why is creating of if Expressions not allowed?

Adam D. Ruppe destructionator at gmail.com
Sun Mar 24 18:59:45 UTC 2019


On Sunday, 24 March 2019 at 18:18:39 UTC, sighoya wrote:
> Hmm..., sounds like bad news. Is there any mixin technology for 
> statements?

mixin() works for statements too.

It is the *context* though. If mixin() appears where the compiler 
expects an expression, it must give an expression. Ditto for 
statements (and for declarations btw). Consider

int main() {
    // compiler expecting a statement here, so
    mixin("if(true) {}"); // allowed

    // but here it is expecting an expression
    return mixin("some_expression");
}

This is also the reason why mixin sometimes requires ; and 
sometimes requires a LACK of ;

int main() {
    int a;
    mixin("a = 10;"); // the ; inside the mixin is required there
    return mixin("a"); // but mixin("a;"); there would be an error
}


Think of mixin() not as pasting code per se, but pasting an AST 
node inside the compiler's data structures. It must be a complete 
node of that tree that can be substituted in.


More information about the Digitalmars-d-learn mailing list