dpure-functiondebug-printmemory-safety

Escaping Safety with Debug Statements


I know of

debug writeln("Some good debug message")

in pure functions but what about functions that I have carefully tagged as @safe or @trusted? DMD currently doesn't allow debug writeln's in those because writeln and similar are currently @system. This is IMHO very frustrating. Is there a clever way to escape safety or do I have to temporarily comment out all my @safe and @trusted tags?


Solution

  • What I do is make my own @trusted debug_writeln.

    @trusted void debug_writeln(T...)(T t) {
          import std.stdio;
          writeln(t);
    }
    

    Since this is @trusted, it will work inside @safe functions without changing anything. You can also just call it writeln and then don't import std.stdio or alias to force the disambiguation.