Last active
August 29, 2015 13:59
-
-
Save takikawa/10555205 to your computer and use it in GitHub Desktop.
Submodule + syntax state behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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