Wrap a call?
Nick Treleaven
nick at geany.org
Sat Feb 21 10:35:46 UTC 2026
On Wednesday, 18 February 2026 at 19:43:38 UTC, JN wrote:
> template WrapCall(string call)
> {
> enum WrapCall = "before(); " ~ call ~ "; after();";
> }
>
> void main()
> {
> mixin WrapCall!"add(1, 2)";
> }
The mixin statement inserts the body of the template into main's
scope, so this just declares a string enum:
```d
void main()
{
enum WrapCall = "before(); " ~ "add(1, 2)" ~ "; after();";
}
```
Note that eponymous templates don't do anything special for mixin
statements.
> This doesn't work, if I change it to mixin(WrapCall!"add(1,
> 2")), it works, why?
Because that string mixin takes an enum string template
instantiation and parses it as if it were code inside main, so
you get:
```d
void main()
{
before(); add(1, 2); after();
}
```
More information about the Digitalmars-d-learn
mailing list