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

FWIW, That functionality's been in the "semitwist d tools" util lib for years. (Although granted, much of the lib has bitrotted by this point.) See "traceVal".

I'd wanted to do a non-mixin version, but that hasn't been possible. That was a major motivator for my suggestion ages ago of "string mixins without the mixin keyword by marking a ctfe func as mixin" but that was rejected in favor of only allowing it for template mixins, not string mixins.

Reply via email to