6.0.1.13
4 Debug
(require sugar/debug) | package: sugar |
Debugging utilities.
syntax
(report expr)
Print the name and value of expr to current-error-port, but also return the evaluated result of expr as usual. This lets you see the value of an expression or variable at runtime without disrupting any of the surrounding code.
For instance, suppose you wanted to see how first-condition? was being evaluted in this expression:
(if (and (first-condition? x) (second-condition? x)) (one-thing) (other-thing))
You can wrap it in report and find out:
(if (and (report (first-condition? x)) (second-condition? x)) (one-thing) (other-thing))
This code will run the same way as before. But when it reaches first-condition?, you willl see in current-error-port:
(first-condition? x) = #t
You can also add standalone calls to report as a debugging aid at points where the return value will be irrelevant, for instance:
(report x) (if (and (report (first-condition? x)) (second-condition? x)) (one-thing) (other-thing))
x = 42
(first-condition? x) = #t
But don’t do this, because the result of the if expression will be skipped in favor of the last expression, which will be the value of x:
(if (and (report (first-condition? x)) (second-condition? x)) (one-thing) (other-thing)) (report x)