On 02/08/2017 04:41 PM, Dan Liebgold wrote:
Hi all -
I have an odd syntax I'm trying to maintain backward compatibility with, but
I'd like to take advantage of keyword parameters to accommodate the
presence/absence/ordering of those parameters.
Here's an example of what I'm trying to do: http://pasterack.org/pastes/88615
Can you think of a better way to translate the syntax to function application?
Note that I must do a little processing of the parameters in my syntax
(represented here by the multiplications).
There are two problems in your macro. First, the arguments to
`keyword-apply` are expressions, so you need to turn your sequences of
keywords and keyword arguments into list-valued expressions. Something
like this:
'(s.key ...) ;; quote okay because keys are literals, not exprs
(list s.value ...) ;; must use list, not quote
Second, `keyword-apply` also requires its arguments to be sorted; so you
would have to do something like this:
(define-syntax (test-syntax stx)
(define-splicing-syntax-class spec
#:datum-literals (:a :b :c)
(pattern (~seq :a n:number)
#:attr key '#:a ;; not syntax, just keyword
#:attr value #'n)
(pattern (~seq :b n:number)
#:attr key '#:b ;; likewise
#:attr value #'(* 2 n))
(pattern (~seq :c n:number)
#:attr key '#:c ;; likewise
#:attr value #'(* 4 n))
)
(syntax-parse stx
([_ s:spec ...]
(define unsorted ;; (Listof (cons Keyword Syntax))
(map cons (attribute s.key) (attribute s.value)))
(define sorted (sort unsorted keyword<? #:key car))
(with-syntax ([((kw . kwarg) ...) sorted])
#'(keyword-apply
test
'(kw ...)
(list kwarg ...)
'())))))
Or you can reuse Racket's `#%app` to do the keyword sorting for you. The
`template` form and its `?@` splicing support makes this more convenient:
(require (for-syntax syntax/parse/experimental/template))
(define-syntax (test-syntax stx)
(define-splicing-syntax-class spec
.... as in your original version ....)
(syntax-parse stx
([_ s:spec ...]
(template
(test (?@ s.key s.value) ...)))))
Ryan
--
You received this message because you are subscribed to the Google Groups "Racket
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.