add naming option to `report`

pull/2/head
Matthew Butterick 10 years ago
parent ea446cfcf5
commit f7a646c6de

@ -1,8 +1,12 @@
#lang racket/base #lang racket/base
(require (for-syntax racket/base racket/syntax))
(provide report) (provide report)
(define-syntax-rule (report expr) (define-syntax (report stx)
(begin (syntax-case stx ()
(displayln (format "~a = ~v" 'expr expr) (current-error-port)) [(_ expr) #'(report expr expr)]
expr)) [(_ expr name)
#'(begin
(displayln (format "~a = ~v" 'name expr) (current-error-port))
expr)]))

@ -10,8 +10,8 @@
Debugging utilities. Debugging utilities.
@defform[(report expr)] @defform*[((report expr) (report expr maybe-name))]
Print the name and value of @racket[_expr] to @racket[current-error-port], but also return the evaluated result of @racket[_expr] as usual. This lets you see the value of an expression or variable at runtime without disrupting any of the surrounding code. Print the name and value of @racket[_expr] to @racket[current-error-port], but also return the evaluated result of @racket[_expr] as usual. This lets you see the value of an expression or variable at runtime without disrupting any of the surrounding code. Optionally, you can use @racket[_maybe-name] to change the name shown in @racket[current-error-port].
For instance, suppose you wanted to see how @racket[first-condition?] was being evaluted in this expression: For instance, suppose you wanted to see how @racket[first-condition?] was being evaluted in this expression:
@ -34,12 +34,12 @@ This code will run the same way as before. But when it reaches @racket[first-con
You can also add standalone calls to @racket[report] as a debugging aid at points where the return value will be irrelevant, for instance: You can also add standalone calls to @racket[report] as a debugging aid at points where the return value will be irrelevant, for instance:
@racketblock[ @racketblock[
(report x) (report x x-before-function)
(if (and (report (first-condition? x)) (second-condition? x)) (if (and (report (first-condition? x)) (second-condition? x))
(one-thing) (one-thing)
(other-thing))] (other-thing))]
@racketerror{x = 42 @racketerror{x-before-function = 42
@(linebreak)(first-condition? x) = #t} @(linebreak)(first-condition? x) = #t}
But be careful — in the example below, the result of the @racket[if] expression will be skipped in favor of the last expression, which will be the value of @racket[_x]: But be careful — in the example below, the result of the @racket[if] expression will be skipped in favor of the last expression, which will be the value of @racket[_x]:
@ -49,3 +49,5 @@ But be careful — in the example below, the result of the @racket[if] expressi
(one-thing) (one-thing)
(other-thing)) (other-thing))
(report x)] (report x)]

Loading…
Cancel
Save