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

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

On Jul 17, Gregory Woodhouse wrote:
> (Actually, I'd like the module to be something I can read from a file  
> port at runtime)
> 
> My initial attempt was
> 
>  > (define md (quote
>                (module m scheme/base
>                  (define x 11))))
>  > (define ns (module->namespace ....
> 
> But here I'm stuck. By itself, md or 'md isn't a legitimate module
> path, but I'd like to create a namespace for eval, so I can evaluate
> expressions in the module environment.

You will need to use `eval' if you want to do that:

  (eval md)

will create the module code, and

  (eval '(require 'm))

will require it into the current namespace.  Note that 'foo is used
for modules that were created dynamically.  To get the module's
namespace you need to quote the module's spec, which means a double
quote is needed:

  (define ns (module->namespace ''m))
  (eval 'x ns)

[Usual `eval' disclaimers apply.]

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


Posted on the users mailing list.