On Wednesday, 29 November 2023 at 13:31:14 UTC, DLearner wrote:
```
Error: found `End of File` when expecting `;` following statement
```

If an extra ; is added:
```
   }(` ~ strStartPtr ~ `,` ~ strPLPtr ~ `);`;
```

it works but doesn't seem correct.

This is an annoying limitation of the D compiler.

The root of the problem is that there is an ambiguity in D's grammar. When the compiler sees

    mixin(whatever);

...it cannot tell whether it's supposed to be a [Mixin Statement][1], or an [Expression Statement][2] that contains a [Mixin Expression][3].

If it's a Mixin Statement, then there should be a semicolon inside the mixin. If it's an Expression Statement with a Mixin Expression, then there shouldn't be a semicolon inside the mixin.

To resolve this ambiguity, the compiler (currently) *assumes* that it's always a Mixin Statement, which means that it will always require a semicolon inside the mixin.

As a result, it is impossible to write a string mixin that can be used as both a statement and an expression.

If you have an expression mixin and you would like to use it as a statement, you can work around this limitation by adding `cast(void)` in front of the mixin:

    cast(void) mixin(whatever);

This forces the compiler to parse the line as an Expression Statement containing a [Cast Expression][4], but does not otherwise change the meaning of the code.

[1]: https://dlang.org/spec/statement.html#mixin-statement
[2]: https://dlang.org/spec/statement.html#expression-statement
[3]: https://dlang.org/spec/expression.html#mixin_expressions
[4]: https://dlang.org/spec/expression.html#cast_expressions
  • mixin issue DLearner via Digitalmars-d-learn
    • Re: mixin issue Dennis via Digitalmars-d-learn
    • Re: mixin issue Paul Backus via Digitalmars-d-learn

Reply via email to