|
| 1 | +#lang syntax-parse-example |
| 2 | +@require[ |
| 3 | + (for-label racket/base syntax/parse syntax/parse/define syntax-parse-example/displaylns/displaylns)] |
| 4 | + |
| 5 | +@(define displaylns-eval |
| 6 | + (make-base-eval '(require syntax-parse-example/displaylns/displaylns) '(define a-port (current-output-port)))) |
| 7 | + |
| 8 | +@title{@tt{displaylns}} |
| 9 | + |
| 10 | +@; ============================================================================= |
| 11 | + |
| 12 | +@defmodule[syntax-parse-example/displaylns/displaylns]{} |
| 13 | +@stxbee2021["Lazerbeak12345" 2] |
| 14 | + |
| 15 | +@defform[(displayln* expr ...)]{ |
| 16 | + This macro is intended to make debugging easier by allowing a programmer to |
| 17 | + print a batch of values all in one go much like Python's |
| 18 | + @hyperlink["https://docs.python.org/3/library/functions.html#print" "print"]. |
| 19 | + |
| 20 | + To change the output port use @racket[parameterize]. |
| 21 | + |
| 22 | + @examples[#:eval displaylns-eval |
| 23 | + (displayln* 1 2 3 4 5 '(this is a list of datums 1 2 3 "hi")) |
| 24 | + (parameterize ([current-output-port a-port]) |
| 25 | + (displayln* 1 2 '(a b))) |
| 26 | + ] |
| 27 | + |
| 28 | + With @racket[define-syntax-parse-rule], this macro is a one-liner: |
| 29 | + |
| 30 | + @racketfile{displaylns.rkt} |
| 31 | + |
| 32 | + A function could express the same behavior. |
| 33 | + Furthermore, using a function instead would help reduce code size --- |
| 34 | + assuming the compiler does not inline every call to @racket[displayln*]. |
| 35 | + That said, a macro has two advantages for this debugging tool: |
| 36 | + @itemize[ |
| 37 | + @item{ |
| 38 | + You can easily to hide all the debug expressions by changing the macro |
| 39 | + body to @racket[(void)]. |
| 40 | + } |
| 41 | + @item{ |
| 42 | + You might extend the macro to compute source locations from its arguments. |
| 43 | + } |
| 44 | + ] |
| 45 | + |
| 46 | +} |
0 commit comments