Getting code in strings: __EXPRESSION_OF__

Paul Backus snarwin at gmail.com
Tue Jul 18 17:37:26 UTC 2023


On Tuesday, 18 July 2023 at 13:33:49 UTC, Quirin Schroll wrote:
> To a limited extent, it can be replicated using `alias` 
> template parameters:
> [...]
> However, that only works with identifiers.

The general-case replacement for this is a string mixin:

```d
string myAssert(string condition, string message = "assertion 
failed: ")
{
     return `assert(` ~ condition ~ `, q"(` ~ message ~ condition 
~ `)");`;
}

void main()
{
     mixin(myAssert("2 + 2 == 5"));
}
```


More information about the Digitalmars-d mailing list