[format] allow identifier macro
This commit is contained in:
parent
e280ce12a5
commit
d3f45e0aba
10
format.rkt
10
format.rkt
|
@ -40,11 +40,17 @@
|
|||
(if t (quasisyntax/loc #'f (ann #,a #,t)) a))])
|
||||
(quasisyntax/loc #'f
|
||||
(format template #,@arg+*))))]
|
||||
[f:id
|
||||
(syntax/loc #'f format)]
|
||||
[(f tmp arg* ...)
|
||||
(syntax/loc #'f (format tmp arg* ...))]))
|
||||
|
||||
(define-syntax-rule (printf: arg* ...)
|
||||
(display (format: arg* ...)))
|
||||
(define-syntax printf:
|
||||
(syntax-parser
|
||||
[f:id
|
||||
(syntax/loc #'f printf)]
|
||||
[(f arg* ...)
|
||||
(syntax/loc #'f (display (format: arg* ...)))]))
|
||||
|
||||
;; -----------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -39,4 +39,16 @@
|
|||
(parameterize ([error-print-width 4])
|
||||
(test-format: ["~a ~.a" "hello" "world"] "hello w..."))
|
||||
|
||||
;; Higher-order use: should work, but lose typechecking
|
||||
(check-equal?
|
||||
((lambda ([f : (-> String Any String)] [x : Any]) (f "hello ~a" x))
|
||||
format: 'world)
|
||||
"hello world")
|
||||
|
||||
(check-exn exn:fail:contract?
|
||||
(lambda ()
|
||||
((lambda ([f : (-> String Any Void)])
|
||||
(f "~b" "not-a-number"))
|
||||
printf:)))
|
||||
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user