[plt-scheme] cannot modify module-bound variables

From: Yin-So Chen (yinso.chen at gmail.com)
Date: Mon Apr 16 19:30:36 EDT 2007

Hi all -

Tying together what I've learned so far I ran into problems trying to create
a function dispatcher without having the client aware of the existence of
the dispatch table (implemented as a hash-table).  The code works until I
try to abstract the functionality to reside in a module, at which I get the
following error:

define-values: cannot change identifier that is instantiated as a module
constant: dispatcher

How can I get around this problem?  Making dispatcher a parameter object
doesn't seem to be of help either.  Basically the code has the following:

1) dispatcher as a hash-table
2) register & dispatch macros that hides the existence of the dispatcher
3) define-action that wraps around creating a function and register the
function with the dispatcher (i.e. without the client having to manual
register)

Below is the code in question...

(module foo mzscheme

  (define dispatcher (make-parameter (make-hash-table 'equal))) ; run-time
dispatcher...

  (define-syntax (register stx)
    (syntax-case stx ()
      ((_ key value)
       #'(hash-table-put! dispatcher key value))))

  (define-syntax (dispatch stx)
    (syntax-case stx ()
      ((_ key args ...)
       #'((hash-table-get dispatcher key) args ...))))

  (define-syntax (define-action stx)
    (syntax-case stx ()
      ((_ (name arg1 ...) body ...)
       #`(begin (define (name arg1 ...)
                  body ...)
                (register (quote name) name)))))

  (provide register
           dispatch
           dispatcher
           define-action))

(module bar mzscheme
  (require foo)
  (define-action (test a b) (+ a b))
  (display (test 1 2))
  (display (dispatch 'test 1 2)))

(require bar) ; error = define-values: cannot change identifier that is
instantiated as a module constant: dispatcher

Thanks,
yinso

-- 
http://www.yinsochen.com
...continuous learning...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070416/b5b43d3b/attachment.html>

Posted on the users mailing list.