[plt-scheme] make-namespace in embedded scheme

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Feb 13 17:08:33 EST 2007

At Tue, 13 Feb 2007 09:15:21 +0000, support at taxupdate.com wrote:
> I'm having a little trouble understanding the use of scheme_make_namespace
> in my program.  Here's a sample:
> 
> #include "scheme.h"
> 
> int main(int argc, char ** args)
> {
>    Scheme_Env * sc1 = scheme_basic_env();
>    Scheme_Env * sc2 = (Scheme_Env *)scheme_make_namespace(0, NULL);
> 
>    scheme_eval_string("(define x 1)", sc1);
>    scheme_eval_string("(define x 2)", sc2);
> 
>    Scheme_Object * p1 = scheme_eval_string("x", sc1);
>    printf("p1 = %d\n", SCHEME_INT_VAL(p1));
>    return 0;
> }
> 
> 
> I was expecting output of "1", but I get "2".  My expectation has been
> that sc2 is a completely separate environment from sc1.  Any guidance?

scheme_eval_string() is somehow not always using the given environment
(a.k.a. namespace), and it is instead sometimes using the current
environment. You can work around the problem by setting the current
namespace, as below.

Matthew

----------------------------------------

int main(int argc, char ** args)
{
   Scheme_Env * sc1 = scheme_basic_env();
   Scheme_Env * sc2 = (Scheme_Env *)scheme_make_namespace(0, NULL);

   scheme_eval_string("(define x 1)", sc1);

   /* Switch to sc2 */
   Scheme_Object *current_namespace;
   Scheme_Object *a[1];
   current_namespace = scheme_builtin_value("current-namespace");
   a[0] = (Scheme_Object *)sc2;
   scheme_apply(current_namespace, 1, a);

   scheme_eval_string("(define x 2)", sc2);

   /* Switch back to sc1 */
   a[0] = (Scheme_Object *)sc1;
   scheme_apply(current_namespace, 1, a);

   Scheme_Object * p1 = scheme_eval_string("x", sc1);
   printf("p1 = %d\n", SCHEME_INT_VAL(p1));
   return 0;
}



Posted on the users mailing list.