[format] allow identifier macro

This commit is contained in:
ben 2015-12-13 01:40:51 -05:00
parent e280ce12a5
commit d3f45e0aba
2 changed files with 20 additions and 2 deletions

View File

@ -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* ...)))]))
;; -----------------------------------------------------------------------------

View File

@ -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:)))
)