On Mon, Nov 11, 2013 at 2:21 AM, Jacob Carlborg <doob@me.com> wrote:
On 2013-11-11 11:10, Timothee Cour wrote:
I would really like to use AST macros for the following use case:

myAssert( x < y);
//will print, on failure, a message, along with all values appearing
inside macro myAssert:
x<y failed: x=..., y=...

myAssert( fun(x,y)==z1+z2)
//likewise, but nesting down to all individual variables appearing
inside the macro:
x=..., y=..., fun(x,y)=..., z1=..., z2=..., bar(z1+z2)=...

This would advantageously replace the plethora of unittest helpers found
in other languages, eg: CHECK_EQ, CHECK_LEQ, etc.
Invaluable for debugging or informative unittests and logs.

Agree. That's the first example in the DIP. Although a very simplified version.

yes, I think your initial example becomes more interesting with this, as in its current form it can already be done in current D.

This would make error messages self-documenting:

myAssert(!file.exists );
// "!file. exists" failed: dump of AST:
file: string ="foobar.d"
|_file.exists: bool = false
  |_!file. exists: bool = false

In many cases, this would be so much more useful than an out of date / incomplete string error message, esp w a lot of variables involved.