PasteRack.org
Paste # 59957
2018-04-18 11:19:42

Fork as a new paste.

Paste viewed 469 times.


Embed:

  1. #lang racket/base
  2.  
  3. (require racket/format
  4.          (for-syntax syntax/parse)
  5.          (for-syntax racket/base))
  6.  
  7. (define-syntax (print stx)
  8.   (syntax-parse stx
  9.     (((~literal print) expr)
  10.      (if #t ;; need some condition here that knows whether expr is an integer or a float
  11.          (syntax/loc stx
  12.            (displayln (~r expr #:notation 'positional #:precision '(= 2))))
  13.          (syntax/loc stx
  14.            (printf "~a\n" expr))))))
  15.  
  16. (define-syntax (this stx)
  17.   (syntax-parse stx
  18.     (((~literal this) (expr ...))
  19.      (syntax/loc stx (expr ...))
  20.      )
  21.     (((~literal this) datum)
  22.      (if (exact? (syntax->datum #'datum))
  23.        (printf "we know at compile time that is is an integer\n")
  24.        (printf "we know at compile time that is is a float\n"))
  25.      (syntax/loc stx datum))))
  26.  
  27.  
  28. (print (this (this (this 123))))
  29. (print (this (this (this 456.0))))

=>

we know at compile time that is is an integer

123.00

we know at compile time that is is a float

456.00