On Sunday, 8 September 2024 at 22:01:10 UTC, WraithGlade wrote:
Basically, I want there to be a way to print both an expression
and its value but to only have to write the expression once
(which also aids refactoring). Such a feature is extremely
useful for faster print-based debugging.
Thus, instead of having to write something like this:
```
writeln("1 + 2 == ", 1 + 2);
```
I want to just be able to write this:
```
show!(1 + 2)
```
The only way to write a macro in D that executes code in the
scope where the macro is *invoked* (rather than the scope where
it's *defined*) is to use a `mixin`:
```d
enum show(string expr) = `writeln(q"(` ~ expr ~ `)", " == ", ` ~
expr ~ `);`;
void main()
{
import std.stdio;
mixin(show!"1 + 2"); // 1 + 2 == 3
const int x = 1 + 2;
mixin(show!"x"); // x == 3
}
```