Skip to content

Instantly share code, notes, and snippets.

@takikawa

takikawa/gist:10555205

Last active Aug 29, 2015
Embed
What would you like to do?
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