[racket] Unquote symbol outside quasiquote

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Sep 4 16:35:02 EDT 2012

The macro

 (define-syntax-rule (get-fields '(x ...) obj)
   (list (get-field x obj) ...))

matches and discards a literal quote. Here's a complete example:

 #lang racket

 (define-syntax-rule (get-fields '(x ...) obj)
   (list (get-field x obj) ...))

 (define o (new (class object%
                  (super-new)
                  (field [a 'a-val] [b 'b-val]))))

 (get-fields '(a b) o)

But you could just as well leave the quote off everywhere:

 #lang racket

 (define-syntax-rule (get-fields (x ...) obj)
   (list (get-field x obj) ...))

 (define o (new (class object%
                  (super-new)
                  (field [a 'a-val] [b 'b-val]))))

 (get-fields (a b) o)


Are you including a quote because you mean to allow an arbitrary
expression after `get-fields'? In that case, I think you'd want a
function... but `racket/class' doesn't offer a `dynamic-get-field'
function (analogous to `dynamic-send'), and probably it should.


At Tue, 04 Sep 2012 19:30:48 +0000 (GMT), André Matheus wrote:
> Hi everyone,
> 
> I want to create a macro to get fields of an object specified by a list of 
> symbols. Like
> 
>     (get-fields '(x y) obj) -> (list (get-field x obj) (get-field y obj))
> 
> Now, my macro will receive (quote x) and (quote y), for example. What I want 
> to know
> is, what is the right way to unquote it? Using eval as in
> 
>     (datum->syntax #'lex `(get-field ,(eval (second (syntax->datum 
> #'(get-fields 'y xx)))) xx) #'srcloc)
> 
> removes the quote but I don't think it's the right way to do it.
> 
> Thanks,
> 
> André
> Hi everyone,
> 
> I want to create a macro to get fields of an object specified by a list of symbols. Like
> 
>     (get-fields '(x y) obj) -> (list (get-field x obj) (get-field y obj))
> 
> Now, my macro will receive (quote x) and (quote y), for example. What I want to know
> is, what is the right way to unquote it? Using eval as in
> 
>     (datum->syntax #'lex `(get-field ,(eval (second (syntax->datum #'(get-fields 'y xx)))) xx) 
> #'srcloc)
> 
> removes the quote but I don't think it's the right way to do it.
> 
> Thanks,
> 
> André
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users


Posted on the users mailing list.