Stream: ideas

Topic: dbg for functions?


view this post on Zulip Matthew Griffith (Mar 07 2023 at 18:44):

I was thinking of a case for dbg that might be interesting. Or it might be clumsy, who knows.

What if it was allowed to have dbg directly after the \ for a function declaration.

myTest = \dbg argOne argTwo ->
   argOne + argTwo

This could print out:

dbg: myTest

    argOne : 5
    argTwo : 10

    15

If you were extra fancy, any calls to dbg within that function would be captured in that same block.

myTest = \dbg argOne argTwo ->
   otherValue = 3000

   dbg otherValue

   argOne + argTwo + otherValue
dbg
    argOne : 5
    argTwo : 10

        otherValue : 3000

    15

Maybe that's too far.

Why I think this is kinda interesting is that it mirrors how you write a test in that you see the inputs and outputs to a pure function. And trying to get the inputs and outputs for a specific function is a surprising amount of work to do manually.

view this post on Zulip Brendan Hansknecht (Mar 07 2023 at 18:47):

I like some form of this idea a lot.

view this post on Zulip Anton (Mar 07 2023 at 18:47):

Yeah, me too, I'm not sure about this exact syntax but it seems really nice

view this post on Zulip Brendan Hansknecht (Mar 07 2023 at 18:48):

I want to say do it as

myTest = dbg \argOne, argTwo ->
...

but I'm not sure if that is reasonable. Kinda changes the semantics. That said, debug can't print out lambdas to my knowledge, so I would think it would be safe to do.

view this post on Zulip Richard Feldman (Mar 07 2023 at 19:41):

I like that syntax idea! :point_up:

view this post on Zulip Richard Feldman (Mar 07 2023 at 19:41):

we've talked previously about wanting to expand dbg to work as an expression, e.g. foo = dbg (x + 1)

view this post on Zulip Richard Feldman (Mar 07 2023 at 19:41):

so this could be an extension of that idea: when you do dbg on an expression that's a function, this is the behavior you get

view this post on Zulip Richard Feldman (Mar 07 2023 at 19:42):

which honestly seems like much more useful behavior than having dbg print <function> every time, which doesn't tell you anything at runtime that you didn't already know at compile time :big_smile:

view this post on Zulip Georges Boris (Mar 07 2023 at 23:47):

I have no idea how it is implemented but on Elixir we have contextual aware dbg's - for instance if you put it in a pipeline it will print the output of each step alongside its source code.

I can imagine something like that when used inside a block, printing out each previous statement value that is defined in the same block. So with that in mind this would work:

myTest = \argOne, argTwo ->

   otherValue = 3000

   argOne + argTwo + otherValue

   dbg
myTest = \argOne :: 5, argTwo :: 10 ->

   otherValue = 3000 :: 3000

   argOne + argTwo + otherValue :: 3015

view this post on Zulip Georges Boris (Mar 07 2023 at 23:48):

dbg would read out as "print my context inside this block up until this point"

view this post on Zulip Brendan Hansknecht (Mar 08 2023 at 00:32):

That's really cool

view this post on Zulip Richard Feldman (Mar 08 2023 at 01:50):

whoa, cool idea!

view this post on Zulip Richard Feldman (Mar 08 2023 at 01:50):

I like these explorations of future uses for dbg :rock_on:

view this post on Zulip Richard Feldman (Mar 08 2023 at 01:51):

the original motivation was just to facilitate basic printline debugging, but I love the idea of taking it further!


Last updated: Jun 16 2026 at 16:19 UTC