DIP 50 - AST macros

Rikki Cattermole alphaglosined at gmail.com
Mon Nov 11 02:16:29 PST 2013


On Monday, 11 November 2013 at 10:10:33 UTC, 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.
>
>
>
>
>
> On Mon, Nov 11, 2013 at 1:52 AM, Rikki Cattermole
> <alphaglosined at gmail.com>wrote:
>
>> On Monday, 11 November 2013 at 09:01:31 UTC, dennis luehring 
>> wrote:
>>
>>> One of our targets for AST macros should be the ability to
>>>> replicate roughly linq from c# / .net.
>>>>
>>>> An example syntax for use with AST could be:
>>>>
>>>> auto data = [5, 7, 9];
>>>> int[] data2;
>>>> query {
>>>>   from value in data
>>>>   where value >= 6
>>>>   add to data2
>>>> }
>>>>
>>>> Could be unwrapped to:
>>>>
>>>> auto data = [5, 7, 9];
>>>> int[] data2;
>>>> foreach(value; data) {
>>>>   if (value >= 6) data2 ~= value;
>>>> }
>>>>
>>>
>>> could you add the example to the DIP wiki page
>>>
>>
>> Adding use case section with Linq example.
>> Please remove / modify as required as is first time editing 
>> wiki.

Perhaps an alternative would be:

asserts {
     x < y
     z == z
}

Where the macro would do something like this once converted:

if (!(x < y)) writeln("x < y failed with x=", x, " y= ", y);
if (!(z == z)) writeln("z == z failed with z=", z);

This would also have the benefit of allowing for multiple assert 
statements for one block.


More information about the Digitalmars-d mailing list