On Sunday, 7 February 2016 at 23:26:05 UTC, Andrei Alexandrescu
wrote:
On 02/04/2016 09:46 PM, Tofu Ninja wrote:
On Thursday, 4 February 2016 at 15:33:41 UTC, Andrei
Alexandrescu wrote:
https://github.com/D-Programming-Language/phobos/pull/3971 --
Andrei
People one github were asking for a dump function so they
could do
int a = 5;
dump!("a"); // prints "a = 5"
Here's a working version if anyone wants it but you have to
use it like
mixin dump!("a");
//------------------------------------------------
mixin template dump(Names ... )
{
auto _unused_dump = {
import std.stdio : writeln, write;
foreach(i,name; Names)
{
write(name, " = ", mixin(name),
(i<Names.length-1)?", ":
"\n");
}
return false;
}();
}
unittest{
int x = 5;
int y = 3;
int z = 15;
mixin dump!("x", "y"); // x = 5, y = 3
mixin dump!("z"); // z = 15
mixin dump!("x+y"); // x+y = 8
mixin dump!("x+y < z");// x+y < z = true
}
This is useful and we should include it. How would you improve
the code to allow dumping to a different File than stdout (e.g.
most often stderr)? -- Andrei
Something like this?
import std.stdio : File, stderr, stdout;
template dumpTo(alias output)
if (isInstanceOf!(File, output))
{
mixin template dumpTo(Names ... )
{
auto _unused_dump = {
import std.traits : Select;
foreach (i, name; Names)
{
output.write(name, " = ", mixin(name),
Select!(i < Names.length - 1, ", ", '\n'));
}
return false;
}();
}
}
alias dump = dumpTo!stdout;
alias errDump = dumpTo!stderr;