DIP 50 - AST macros

Timothee Cour thelastmammoth at gmail.com
Mon Nov 11 02:39:53 PST 2013


On Mon, Nov 11, 2013 at 2:21 AM, Jacob Carlborg <doob at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20131111/c8200d92/attachment.html>


More information about the Digitalmars-d mailing list