[plt-scheme] track symbols and respecting the lexical environment

From: YC (yinso.chen at gmail.com)
Date: Wed Aug 15 04:23:01 EDT 2007

Hi all,

I've found needs to track variables/values that I've instantiated to aid
meta programming, and hash-table seems to work well for this purpose.  Below
is a sample for motivation.

;;; the struct that holds the metadata info
(define-struct type (name isa? cast) (make-inspector))

;;; have a register to keep track of the types
(define-values (type:register type:get)
  (let ((register (make-hash-table 'equal)))
    (define (type:register key value)
      (hash-table-put! register key value))
    (define (type:get key)
      (hash-table-get register key #f))))

;;; macro to wrap around the definition + registration
(define-syntax define-type
  (syntax-rules (isa? cast)
    ((_ name (isa? isa?-exp) (cast cast-exp))
     (define name
       (let ((t (make-type 'name isa?-exp cast-exp)))
         (foo:register 'name t)
         t)))))

Of course - any data stored in a hash table would live until the hash table
goes out of scope or until the data gets overwritten, and it doesn't obey
lexical rules.

Are there ways to make it obey lexical rules so the register can behave more
like an actual symbol table?  i.e. if a type is declared within a lexical
scope as it goes out of scope the value is removed from the register.  I
assume that if this is possible there needs to be a way to know that I'm in
a different lexical scope then before.

Any thoughts are appreciated - thanks.

yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070815/72bdc9c7/attachment.html>

Posted on the users mailing list.