better assertions and __FILE__

Lutger lutger.blijdestijn at gmail.com
Fri Aug 17 04:09:12 PDT 2007


Bruce Adams wrote:
> 
> Here are a few more things I've learned on my travels around the various D web-pages.
> 
> __FILE__ & __LINE__ are expanded by the tokeniser not the parser. This implies that they would not be available for use in mixins as they will already have been expanded. This is confirmed with a quick test. So what I'm trying to do is not currently possible.
>

It's possible to use CTFE with string mixins to achieve this. I have 
whipped up something quick, ugly as hell but could be improved using 
std.metastrings.Format:


char[] assertEqual(char[] actual, char[] expected)
{
     return  `if (` ~ actual ~ `!=` ~ expected ~ `) { `
                 `writefln("Equality assertion failed in " ~ __FILE__ ~ 
" at line " ~ std.metastrings.ToString!(__LINE__));`
                 `writefln("expected value: '", ` ~ expected ~ `, "'");` 

                 `writefln("actual value:   '", ` ~ actual ~ `, "'");`
                 `assert(false);}`;
}

used as:

mixin(assertEqual("foo","bar"));


If you can live with a) having to use 'mixin', b) ugliness of compile 
time string manipulation and perhaps c) sometimes hard to follow error 
messages, you can do most of anything you want with CTFE and string mixins.
I think if one would make work of this most of the tediousness could be 
solved with some boilerplate code.



More information about the Digitalmars-d-learn mailing list