Does D have a tool like pySnooper?
Taylor Hillegeist
taylorh140 at gmail.com
Fri Apr 26 14:33:16 UTC 2019
On Friday, 26 April 2019 at 10:22:49 UTC, Bastiaan Veelo wrote:
> 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.
Hey, that's a pretty cool demo. I saw the idea and wondered
rather it could be possible with D. I has some very cool
introspection properties. The Link seems to be broken right now.
But I think this might be worth some development. Tools like this
can really help new users, especially when they are doing things
the o'l fashion way, printf for debugging. I doubt it would be
too much help for things like ranges, but one must pick their
battles.
Thanks.
More information about the Digitalmars-d-learn
mailing list