__ARGS__ : allow access to (stringified) arguments, as C's `#arg` macro

Simen Kjærås simen.kjaras at gmail.com
Fri Jan 19 11:29:38 UTC 2018


On Friday, 19 January 2018 at 08:51:00 UTC, Jacob Carlborg wrote:
> Not sure I understand this feature. Is it something like:
>
> auto foo = 3;
> auto bar = 4;
> log(foo, bar);
>
> Would print?
>
> main.d:3 foo=3
> main.d:3 bar=4
>
> If that's the case then this seems like yet another hack 
> because we don't have AST macros.

The above is trivial:

template log(Args...) {
     auto log(string file = __FILE__, int line = __LINE__)() {
         import std.stdio;
         static foreach (arg; Args) {
             writeln(file, ":", line, " ", arg.stringof, "=", arg);
         }
     }
}

unittest {
     int foo = 3;
     int bar = 4;
     log!(foo, bar);
}

What's hard is getting expressions as text:

unittest {
     int foo = 3;
     int bar = 4;
     // Should print 'main.d:5 foo+bar=7'
     // but fails to compile with 'variable foo cannot be read at 
compile time'
     log!(foo + bar);
}

Actually, if we could alias expressions, this should Just Work™.

--
  Simen


More information about the Digitalmars-d mailing list