Skip to content

Instantly share code, notes, and snippets.

@takikawa
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takikawa/10555205 to your computer and use it in GitHub Desktop.
Save takikawa/10555205 to your computer and use it in GitHub Desktop.
Submodule + syntax state behavior
#lang racket
(require (for-syntax racket/dict syntax/id-table))
;; track stuff in this table
(begin-for-syntax
(define tbl (make-free-id-table)))
;; a binding we can use in the table
(define x 3)
(begin-for-syntax (define q-x #'x))
(begin-for-syntax
;; initially set x -> "outer"
(dict-set! tbl q-x "outer")
(module* sub #f
;; the submodule sees "outer"
(for ([(k v) (in-dict tbl)])
(printf "~a -> ~a~n" k v))
(dict-set! tbl q-x "inner")
;; the submodule now sees "inner"
(for ([(k v) (in-dict tbl)])
(printf "~a -> ~a~n" k v))))
;; this macro just prints what's in the table
(define-syntax (print stx)
(syntax-case stx ()
[(_)
(for ([(k v) (in-dict tbl)])
(printf "~a -> ~a~n" k v))
#'(void)]))
(provide print)
$ racket
Welcome to Racket v6.0.1.1.
-> (require "test.rkt")
-> (print)
#<syntax:/tmp/test.rkt:11:32 x> -> outer
-> (require (submod "test.rkt" sub))
#<syntax:/tmp/test.rkt:11:32 x> -> outer
#<syntax:/tmp/test.rkt:11:32 x> -> inner
-> (print)
#<syntax:/tmp/test.rkt:11:32 x> -> outer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment