PasteRack.org
Paste # 27398
2021-01-18 06:16:33

Fork as a new paste.

Paste viewed 292 times.


Embed:

struggling with basic macro

  1. #lang racket/base
  2.  
  3. (require (for-syntax racket/base
  4.                      racket/struct-info
  5.                      syntax/parse))
  6.  
  7. (define-syntax (struct->hash stx)
  8.   (syntax-parse stx
  9.     [(_ type-id:id instance:expr)
  10.      (with-syntax ([fld-names (struct-field-info-list (syntax-local-value #'type-id))]
  11.                    [fld-accessors (list-ref
  12.                                    (extract-struct-info (syntax-local-value #'type-id)) 3)])
  13.        #`(for/hash ([fld-name #,fld-names]
  14.                     [fld-accessor #,fld-accessors])
  15.            (values fld-name (fld-accessor instance))))]))

=>