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.
[...]
I want to just be able to write this:
```
show!(1 + 2)
```
Try this:
```
import core.interpolation;
void show(Args...)(InterpolationHeader hdr, Args args,
InterpolationFooter ftr) {
import std.stdio;
foreach(arg; args) {
static if(is(typeof(arg) ==
InterpolatedExpression!code, string code))
write(code);
else static if(is(typeof(arg) ==
InterpolatedLiteral!str, string str))
write(str);
else
write(" = ", arg);
}
writeln();
}
```
Used like:
```
void main() {
int a = 5, b = 22;
show(i`$(a), $(b), $(a + b)`);
}
```
Output:
a = 5, b = 22, a + b = 27
You can show as many expressions as you want in there, each
$(....) thing is expanded to `code inside = result`.
So
show(i"$(1 + 2)"); // prints 1 + 2 = 3
The interpolated sequence syntax makes it a bit heavier: that
`i"$( .... )"` around it is required here, but it works anyway.