[plt-scheme] Evaluating code in namespace of calling module

From: Erich Rast (erich at snafu.de)
Date: Thu Mar 17 03:30:38 EST 2005

> I think that `(module->namespace 'bar)' would do what you want.

Thanks for the reply! This yields an interesting error

==> reference to an identifier before its definition: test%

which makes sense, given that at the syntactic location in module foo 
at which test% is evaluated, test% is indeed not yet defined. I suppose 
that any way to work around this would boil down to a really nasty 
hack, so I think I'll stick to some conventional big case statement 
that interprets the external representations of my objects without 
directly evaluating identifiers.

However, I'm still curious whether there generally is some way to 
evaluate code in one module as if it was evaluated in the calling 
module---something like a macro that expands in the namespace of the 
module it is used in, not in the module in which it is defined. (Of 
course, this would somewhat defeat the whole purpose of modules and 
might also raise security issues.)



----- the sample code:

(module foo mzscheme
   (provide my-eval)

   (define (my-eval code namespace)
     (parameterize ((current-namespace namespace))
       (eval code)))

(module bar mzscheme
   (require foo)
   (require (lib "class.ss"))

   (let* ((test% (class* object% ()
                   (public hello-world)
                   (define (hello-world)
                     (display "hello world!")(newline)))))
     (display  (my-eval 'test% (module->namespace 'bar)))

(require bar)

Posted on the users mailing list.