[plt-scheme] Parameterizing Modules with variables

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Apr 20 10:59:40 EDT 2003

On Apr 20, Ed Cavazos wrote:
> [...] Anyways, take Emacs for example. Above the C level, it's just
> a collection of ".el" files all contributing to one big happy
> namespace (AFAICT). [...]

Many times it is not too happy.  For example, I have a calculator
package for Emacs, it is equivalent to xcalc or Windows' calc.exe, but
my package is called "calculator", and the source needs every binding
to have a "calculator-" prefix.  Why not "calc"?  Because there is one
already (and that one is *much* bigger...).


On Apr 20, Matthew Flatt wrote:
> We use parameters for this kind of task. It's hadly any extra work
> at the definition site:
> [...]

And even that small extra work can be eliminated:

  (module fake-settable-vars mzscheme
    (provide defvar)
    (define-syntax (defvar stx)
      (syntax-case stx ()
        ((_ var val) (identifier? #'var)
         (with-syntax ((param (datum->syntax-object
                               #'var
                               (string->symbol
                                (string-append (symbol->string (syntax-e #'var))
                                               "-param"))
                               #'var)))
           #'(begin (define param (make-parameter val))
                    (define-syntax var
                      (make-set!-transformer
                       (lambda (stx)
                         (syntax-case stx (set!)
                           ((set! var1 val1) #'(param val1))
                           ((var . xs)       #'((param) . xs))
                           (var              #'(param))))))))))))

which can be used like this:

  (module foo mzscheme
    (require fake-settable-vars)
    (provide blah)
    (defvar blah #f))

[I guess it wouldn't be too hard to attach a property to defvar'ed
identifiers and change `let' to handle them correctly too...]

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.