[racket] free-id-table. Is it a bug?
On 04/03/2014 01:26 AM, Roman Klochkov wrote:
> #lang racket
> (require (for-syntax syntax/id-table))
>
> (define-for-syntax table (make-free-id-table))
>
> (define-syntax (save-and-define stx)
> (syntax-case stx ()
> [(_ ID) (free-id-table-set! table #'ID 1) #'(define ID 1)]))
The problem is that save-and-define puts ID in the table, but then the
code it produces (the definition) changes the binding of ID, so when you
try to look it up the two versions of ID might not match.
The fix is to put the table update after the definition:
(define-syntax (save-and-define stx)
(syntax-case stx ()
[(_ ID)
#'(begin
(define ID 1)
(begin-for-syntax
(free-id-table-set! table #'ID 1)))]))
Or if you want this to work in internal definition contexts:
(define-syntax (save-and-define stx)
(syntax-case stx ()
[(_ ID)
#'(begin
(define ID 1)
(define-syntaxes ()
(begin0 (values) (free-id-table-set! table #'ID 1))))]))
Ryan