Question

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?

Was it helpful?

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.

OTHER TIPS

A generic solution

import std.stdio;

void main () @safe pure
{
    auto str = trustedPureDebugCall!readln();
    trustedPureDebugCall!writeln(str);
}

debug auto trustedPureDebugCall (alias fn, A...) (A args) @trusted pure
{
     debug return fn(args);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top