> On Apr 27, 2016, at 2:38 PM, Matthew Butterick <m...@mbtype.com> wrote: > > For the first time, I used `syntax-local-introduce` to solve a problem. But I > don't understand whether I'm using it correctly or just relying on a spooky > side effect. (I did the read the docs, but I'm looking for a higher-level > "why this exists and what its idiomatic use is") > > The problem I had was how to make two macros cooperate: one macro defines a > variable, and another macro uses it. One can re-introduce the variable > unhygienically each time. Or, apparently, you can use > `syntax-local-introduce` once at the moment of binding. (Sample code below > shows these two options.) > > I understand that the two options are not the same in terms of the use site. > Introducing a binding unhygienically makes it available to other code at the > use site as if it had been defined there; `syntax-local-introduce` does not. > > I suppose I'm wondering if a) this "cooperation" among macros I'm enjoying is > simply a lucky side effect of both being in the same module
Well, it's not even that. This doesn't work: (let () (define-sli-foo) (let () (invoke-sli-foo))) I had expected that changing invoke-sli-foo to this would help: (define-simple-macro (invoke-sli-foo) #:with sli-foo (syntax-local-introduce #'foo) (slo-foo)) But it didn't. Hm. Although this definition of invoke-sli-foo works with define-sli-foo: (which uses syntax-local-introduce) (define-simple-macro (invoke-sli-foo) #:with sli-foo (datum->syntax this-syntax 'foo) (sli-foo)) And it works with with and without that let. I'm not sure why (datum->syntax this-syntax 'foo) would work and (syntax-local-introduce #'foo) wouldn't. Alex Knauth > and thus sharing a 'local' environment into which the identifier is being > 'introduced', and if so b) there's a better way to go about this than > `syntax-local-introduce`. > > > > <Screen Shot 2016-04-27 at 11.30.20 AM.gif> > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > #lang racket > (provide (all-defined-out)) > > (define-syntax (define-sli-foo stx) > (syntax-case stx () > [(_) > (with-syntax ([sli-foo (syntax-local-introduce #'foo)]) > #`(begin > (define (sli-foo) 'sli-worked)))])) > > (define-syntax-rule (invoke-sli-foo) > (foo)) > > (define-syntax (define-ds-foo stx) > (syntax-case stx () > [(_) > (with-syntax ([ds-foo (datum->syntax stx 'foo)]) > #`(begin > (define (ds-foo) 'ds-worked)))])) > > (define-syntax (invoke-ds-foo stx) > (syntax-case stx () > [(_) > (with-syntax ([ds-foo (datum->syntax stx 'foo)]) > #'(ds-foo))])) > > > (module+ test > (require rackunit) > (check-equal? > (let () > (define-sli-foo) > (invoke-sli-foo)) 'sli-worked) > (check-equal? > (let () > (define-ds-foo) > (invoke-ds-foo)) 'ds-worked)) -- 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.