[racket] help with syntax-splicing in small macro
Hello Danny,
Thanks for the help. The domain was about trying to get the output of a csv into
a struct. I had already solved (badly) the problem with
(define-syntax list->tst
(syntax-rules ()
[(_ attr-list) (apply tst (syntax->datum #`(#, at attr-list)))]))
But solving it directly through the template is much better.
Thanks again.
On Wed, Sep 19, 2012 at 6:45 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
> 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.
--
Eduardo Bellani
"Resolve to serve no more, and you are at once freed."