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)
             syntax/parse/define)

    (begin-for-syntax
      (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))
                              src
                              src))])))

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

    (define-syntax-parser do-splicing-parameterize
      [(_ parameterization:expr body:expr)
       (syntax-parse (local-expand #'body (syntax-local-context)
                                   (kernel-form-identifier-list))
         #:literal-sets [kernel-literals]
         [(begin new-body ...)
          (syntax/loc/props this-syntax
            (begin
              (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}
           . _)
          this-syntax]
         [expr
          (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])
      (my-param)
      (define x (my-param))
      (define (f) (my-param)))

    x
    (f)

...the output is:

    #t
    #t
    #f

...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
program:

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

    (my-param)

...produces:

    42
    #f

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.

Alexis

-- 
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.

Reply via email to