[plt-scheme] Can I create module on the fly?

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Jul 17 16:50:58 EDT 2008

On Jul 17, Gregory Woodhouse wrote:
> On Jul 17, 2008, at 1:17 PM, Eli Barzilay wrote:
> > [Usual `eval' disclaimers apply.]
> That's what I was afraid of.
> The idea here (and I'm really just experimenting at this stage) is
> to set up a network server that can read lists, apply a function to
> them and write back the resulting expression.

You mean apply a function that is specified as part of the input, I

> The trick is that I want to be able to deploy these functions
> without restarting the server (much as you would with EJBs in
> GlassFish or WebLogic). I'm not sure quite how to package these "tea
> leaves", as I call them, but I thought modules would be a good
> starting point.

Well, one thing that you can do is keep one module per function.  For
example, "foo.ss" has

  #lang scheme
  (provide run)
  (define (run x y z)
    (* (+ x y) z))

when you read the list in, you can do something like this (warning:
untested code):

  (define (process-call name args)
    (unless (symbol? name) (error "malformed request"))
    (let ([file (format "/some/dir/~a.ss" name)])
      (unless (file-exists? file) (error "unknown call"))
      (let ([run (dynamic-require `(file ,file) 'run)])
        (unless (procedure? run) (error "bad module code"))
        (unless (procedue-arity-includes run (length args))
          (error "wrong number of arguments"))
        (apply run args))))

This way you can easily add new "verbs" to the directory with no
restarts needed.  The only catch is that if you redefine an existing
module then you will need to restart.  See the code at
"collects/handin-server/private/reloadable.ss" for an example of code
that reloads a module when the file changes (something like
`auto-reload-procedure' seems like it can be useful).

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

Posted on the users mailing list.