I have a handy dump function (copied from internet, most likely from https://forum.dlang.org/) that I'm using to investigate variables.
I just noticed it doesn't respect scopes in all cases (see below). Why and how I can fix it to get the expected results ? Or is the function fundamentally flawed ? dump has been very valuable when I have been learning D.
I'm using DMD64 D Compiler v2.083.0 on Linux.
Expected results when compiled with -debug:
(immutable(int) x = 1)
(immutable(immutable(char)[]) x = A)
(immutable(double) x = 1.1)
but instead got:
(immutable(int) x = 1)
(immutable(int) x = 1)
(immutable(double) x = 1.1)
The code:
void dump(alias variable)()
{
import std.stdio : writefln;
writefln("(%s %s = %s)",
typeid(typeof(variable)),
variable.stringof,
variable);
}
void main()
{
{
immutable x = 1; debug dump!x;
}
{
immutable x = "A"; debug dump!x;
}
{
void f() { immutable x = 1.1; debug dump!x; }
f();
}
}