adding example to the prose about using (for-syntax (for-syntax ...))

This commit is contained in:
Danny Yoo 2011-08-10 13:18:03 -04:00 committed by Vincent St-Amour
parent b72977963c
commit 91068e4085

View File

@ -445,7 +445,38 @@ the right-hand side of the inner @racket[define-syntax] is in the
@deftech{meta-compile phase level}, also known as @deftech{phase level
2}. To import @racket[syntax-case] into that phase level, you would
have to use @racket[(require (for-syntax (for-syntax racket/base)))]
or, equivalently, @racket[(require (for-meta 2 racket/base))].
or, equivalently, @racket[(require (for-meta 2 racket/base))]. For example,
@codeblock|{
#lang racket/base
(require ;; This provides the bindings for the definition
;; of shell-game.
(for-syntax racket/base)
;; And this for the definition of
;; swap.
(for-syntax (for-syntax racket/base)))
(define-syntax (shell-game stx)
(define-syntax (swap stx)
(syntax-case stx ()
[(_ a b)
#'(let ([tmp a])
(set! a b)
(set! b tmp))]))
(syntax-case stx ()
[(_ a b c)
(let ([a #'a] [b #'b] [c #'c])
(when (= 0 (random 2)) (swap a b))
(when (= 0 (random 2)) (swap b c))
(when (= 0 (random 2)) (swap a c))
#`(list #,a #,b #,c))]))
(shell-game 3 4 5)
(shell-game 3 4 5)
(shell-game 3 4 5)
}|
Negative phase levels also exist. If a macro uses a helper function
that is imported @racket[for-syntax], and if the helper function