On Monday, 2 November 2015 at 09:16:09 UTC, Nordlöw wrote:
On Monday, 2 November 2015 at 09:02:28 UTC, Gary Willoughby
wrote:
On Monday, 2 November 2015 at 08:23:16 UTC, Nordlöw wrote:
I need `T` to be an alias in order for .stringof to work.
typeof(T).stringof
No, I want the variable name from the calling scope.
This works for a single argument.
void show(alias arg, string file = __FILE__, uint line =
__LINE__, string fun = __FUNCTION__)()
{
import std.stdio: writeln;
try
{
debug writeln(file, ":",line, ":" /* , ": in ",fun */,
" debug: ", arg.stringof, " is ", arg);
}
catch (Exception) { }
}
unittest
{
int x = 11;
show!x;
}
Prints
dbg.d:80: debug: x is 11
My try at variadic
template show(Args...)
{
void show(string file = __FILE__, uint line = __LINE__,
string fun = __FUNCTION__)()
{
import std.stdio: write, writeln;
try
{
debug write(file, ":",line, ":" /* , ": in ",fun
*/, " debug: ");
foreach (const i, Arg; Args)
{
if (i) debug write(", "); // separator
debug write(Arg.stringof, " is ", Arg);
}
debug writeln();
}
catch (Exception) { }
}
}
fails with compilation error
dbg.d(83,5): Error: variable x cannot be read at compile time
Why can't I make Args a sequence of aliases?
Works for me on multiple compilers. To be precise, this worked:
template show(Args...)
{
void show(string file = __FILE__, uint line = __LINE__,
string fun = __FUNCTION__)()
{
import std.stdio: write, writeln;
try
{
debug write(file, ":",line, ":" /* , ": in ",fun */,
" debug: ");
foreach (const i, Arg; Args)
{
if (i) debug write(", "); // separator
debug write(Arg.stringof, " is ", Arg);
}
debug writeln();
}
catch (Exception) { }
}
}
unittest
{
int x = 11;
show!x;
}