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

From: Erich Rast (erich at snafu.de)
Date: Mon Mar 14 03:22:49 EST 2005

I'd like to evaluate code by a function or macro that is defined in a 
module foo, but the code should be evaluated with respect to all of the 
bindings that are currently active in the module bar that calls the 
custom evaluation function required from foo. So far, I've tried 
passing (current-namespace) to the evaluation function and then use 
parameterize in module foo, but this doesn't seem to work. Perhaps it 
doesn't work because I use let*?

The structure is as follows:

(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)))))
     (object? (my-eval 'test% (current-namespace)))
     )
   )

(require bar)

==> reference to undefined identifier: test%

Why doesn't this work?

(You might ask why I want to do this. In reality, bar is the test unit 
of a simple object serialization scheme, whereas foo provides functions 
externalize and internalize. Part of the latter is my-eval, and my-eval 
is a safe evaluation function that should take the 'class-symbol' in an 
external representation and yields an instance of the class according 
to the way it is defined in bar. The idea is that the program itself 
has full control over the class definition, so only a symbol and not 
the whole class implementation is in the external file, but I thought 
that a big case statement that manually interprets the 'class symbol' 
is a bit clumsy if I can just evaluate it.)

Best regards,

Erich



Posted on the users mailing list.