[racket] help with syntax-splicing in small macro

From: Danny Yoo (dyoo at hashcollision.org)
Date: Wed Sep 19 17:45:11 EDT 2012

On Wed, Sep 19, 2012 at 3:10 PM, Eduardo Bellani <ebellani at gmail.com> wrote:
> Hello list.
>
> Could anyone give me a hand and explaing why this
>
> #lang racket
> (struct tst (attr1 attr2))
>
> (define-syntax (list->struct stx)
>   (syntax-case stx ()
>     [(_ attr-list) #`(tst #, at attr-list)]))


Do you have an example of a use where this breaks?

I suspect that you need to refer to attr-list in the syntactic environment, so
:

(define-syntax (list->struct stx)
  (syntax-case stx ()
    [(_ attr-list) #`(tst #,@#'attr-list)]))



But you might be able to handle the splicing directly through the
template, without needing to quasiquote:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket
(struct tst (attr1 attr2))

(define-syntax (list->struct stx)
  (syntax-case stx ()
    [(_ (attr-list ...))
     #'(tst attr-list ...)]))
; ;Example
(list->struct (3 4))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Also, is this just for learning how to write macros, or are you trying
to serialize structures?  If you're just trying to get structures
serializable, the documentation at
http://docs.racket-lang.org/reference/serialization.html may help.

Posted on the users mailing list.