[plt-scheme] Easy module switching?

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Jun 28 13:29:38 EDT 2007

On Jun 28, Matthias Felleisen wrote:
> On Jun 28, 2007, at 11:38 AM, Henk Boom wrote:
> > Basically I want to be able to swap between different versions of
> > a module from a central location, preferably without having to
> > actually rename the module or change the source file(s). The
> > command line would be a convenient place to be able to set it
> > from. For example, one solution would be to have all of my files
> > require a new "router" module which would require one of the
> > alternatives depending on which I wanted to use. However, to
> > switch between them I would actually have to change the source of
> > this router module. I want to know if there is a more convenient
> > alternative.
> [...]
> I did implement the same interface more than once -- and ended up
> editing source code.
> I did consider writing the main script so that it would switch
> (copy) the module files into the proper location before running the
> program. But this solution looked so inelegant that I just couldn't
> bring myself to do it.

How about this:

  (module x mzscheme
    (define-syntax (env-require stx)
       stx `(require ,(if (getenv "DEBUG") "x1.scm" "x2.scm")) stx))
    (printf ">>> ~s\n" (foo 1 2)))


(And similar variations -- using `command-line-arguments', making
`env-require' be like a plain require that shoves "-debug" into the
file name etc.)

The pitfall here is that this module should not be compiled.

Another option is to use `dynamic-require' for one of two files,
provided that there are a single (or very few) entry points.  It's
possible to use macros to make it easy to use `dynamic-require' with
many bindings -- but going there will probably end up with
descriptions that are a bunch of names -- and that will lead to
rediscovering units...

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

Posted on the users mailing list.