PasteRack.org
Paste # 14205
2019-10-12 10:49:39

Fork as a new paste.

Paste viewed 385 times.


Embed:

structs in syntax-parse -- how to match on a struct and do something with it?

  1. #lang racket
  2.  
  3. (require (for-syntax syntax/parse))
  4.  
  5. (begin-for-syntax
  6.   (define-struct object (colour size))
  7.   (define ball (object 'red 'small))
  8.  
  9.   (define-syntax-class struct
  10.     [pattern s #:fail-unless (struct? (syntax-e #'s))
  11.              "Expected struct?"]))
  12.  
  13. (define-syntax (touch-struct stx)
  14.   (syntax-parse stx
  15.     [(_ obj:struct)
  16.      #`#,(object-colour #'obj)]))
  17.  
  18. (define-syntax (run stx)
  19.   (syntax-parse stx
  20.     [(_)
  21.      #'(touch-struct ball)]))
  22.  
  23. #;(run) ;why does this fail?

=>