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

 



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to