On Sun, 02 Sep 2012 19:14:07 +0200, Peter Alexander <peter.alexander...@gmail.com> wrote:

One thing that's always bothered me is that I have to use std.string.format to produce useful assert messages:

assert(x == 1, format("x doesn't equal 1, it is %d", x));


Of course, I tried to solve this by producing my own assert-like function wrapper:

void myAssert(Args...)(lazy bool condition, lazy Args args)
{
     assert(condition, format(args));
}


That's all good, but the problem now is that I get the wrong file and line in the assert message, so I tried to use the default arg __FILE__ and __LINE__ trick:

void myAssert(Args...)(lazy bool condition, lazy Args args, int line = __LINE__, string file = __FILE__)
{
     if (!condition)
     {
         writeln("Assertion failed @ %s:%d", file, line);
         writefln(args);
         exit(1);
     }
}


But I can't have default arguments because of the variadic arguments!

Can anyone think of a way around this?

void myAssert(int line = __LINE__, string file = __FILE__, Args...)(lazy bool condition, lazy Args args) {
    if (!condition)
    {
        writefln("Assertion failed @ %s:%d", file, line);
        writefln(args);
        exit(1);
    }
}


--
Simen

Reply via email to