It might make sense to `(set! new-parameterization #f)` at the end so that the parameterization (and the values it holds) can be GC'd sooner when splicing-parameterize is used at top level or module level.


On 1/24/18 6:00 AM, Alexis King wrote:
Here is an implementation of a version of splicing-parameterize that
uses local-expand, a la other forms in racket/splicing:

    #lang racket

    (require (for-syntax syntax/kerncase)
             (for-meta 2 racket/base)

      (define-syntax (syntax/loc/props stx)
        (syntax-case stx ()
          [(_ src-expr template)
           #`(let ([src src-expr])
               (datum->syntax (quote-syntax #,stx)
                              (syntax-e (syntax template))

    (define-syntax-parser splicing-parameterize
      [(_ ([{~var param (expr/c #'parameter?)} value:expr] ...)
          body ...)
       (if (eq? (syntax-local-context) 'expression)
           #'(parameterize ([param.c value] ...)
               body ...)
               (define new-parameterization
                 (parameterize ([param.c value] ...)
               (do-splicing-parameterize new-parameterization body)

    (define-syntax-parser do-splicing-parameterize
      [(_ parameterization:expr body:expr)
       (syntax-parse (local-expand #'body (syntax-local-context)
         #:literal-sets [kernel-literals]
         [(begin new-body ...)
          (syntax/loc/props this-syntax
              (do-splicing-parameterize parameterization new-body)
         [(define-values ids rhs)
          (syntax/loc/props this-syntax
            (define-values ids
              (call-with-parameterization parameterization
                                          (thunk rhs))))]
         [({~or begin-for-syntax define-syntaxes module module*
            #%require #%provide #%declare}
           . _)
          (syntax/loc/props this-syntax
            (call-with-parameterization parameterization
                                        (thunk expr)))])])

I have not extensively tested it, but it seems to work okay for simple
programs. For example, given the following program:

    (define my-param (make-parameter #f))

    (splicing-parameterize ([my-param #t])
      (define x (my-param))
      (define (f) (my-param)))


...the output is:


...which I believe makes sense, since the dynamic adjustment of my-param
does not affect its use within the internal definition of a procedure.

Additionally, mutating a parameter does the right thing, so this

    (splicing-parameterize ([my-param #t])
      (my-param 42)




This seems like something that would make sense in racket/splicing,
though it’s different from the other forms, since they are all lexical
binding forms (even splicing-syntax-parameterize), whereas parameterize
is not.


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 
For more options, visit

Reply via email to