Does D have a tool like pySnooper?

Bastiaan Veelo Bastiaan at Veelo.net
Fri Apr 26 10:22:49 UTC 2019


On Friday, 26 April 2019 at 08:35:57 UTC, Bastiaan Veelo wrote:
> On Thursday, 25 April 2019 at 08:44:14 UTC, Dennis wrote:
>> On Monday, 22 April 2019 at 16:24:53 UTC, Taylor Hillegeist 
>> wrote:
>>> Or would this not be easy at all with D?
>>
>> I don't think so. While there are lots of traits for 
>> introspection of declarations, there is no way to introspect 
>> lines of code. The whole function
>> would need to be wrapped into a mixin, and the D code would 
>> need to be parsed
>> at compile time for this to work.
>
> Yes, but I think that might be doable. You wouldn't need a full 
> blown D parser, just one that can identify statements (`;` 
> being an important clue). Not sure whether __LINE__ will be 
> meaningful inside a mixin, though, but that might also be 
> fixable. It would be an interesting challenge.
>
> Bastiaan.

Proofing the concept:
---
mixin(snoop(q{
     int fun(int a, int b)
     {
         int c = 3;
         foreach (i; 1 .. 5)
         {
             a += i;
         }
         int d = a + b + c;
         return d;
     }
}));
---

Output:
2019-Apr-26 10:33:46.0935135 executed line 7:	        int c = 3;
2019-Apr-26 10:33:46.0936716 executed line 10:	            a += i;
2019-Apr-26 10:33:46.0937348 executed line 10:	            a += i;
2019-Apr-26 10:33:46.0937963 executed line 10:	            a += i;
2019-Apr-26 10:33:46.0938583 executed line 10:	            a += i;
2019-Apr-26 10:33:46.0939622 executed line 12:	        int d = a 
+ b + c;

https://run.dlang.io/is/Go97hQ

Bastiaan.


More information about the Digitalmars-d-learn mailing list