On Sunday, 2 September 2012 at 20:30:26 UTC, Timon Gehr wrote:
On 09/02/2012 07:14 PM, Peter Alexander 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?

I don't think there is a deep reason why IFTI shouldn't succeed in this case.

Consider:

myAssert(false, "%d", 1);

What is Args? "%d", 1 could refer to the optional arguments, or the variadic arguments. Both match.

Reply via email to