context and other context variables

Started by frontera000, August 31, 2006, 12:55:53 PM

Previous topic - Next topic

frontera000


(setq main-var 1)

(define (make-function-context)
    (let (temp (append (lambda)  (list (first (args 1))) (rest (args 1))))
      (def-new 'temp (sym (args 0) (args 0)))))

(define (function-template (arg1 arg2))
  (setq myvar arg1)
  (setq MAIN:main-var arg2))

(make-function-context 'foo (0 function-template))

 


when this is run foo:foo looks like this:


Quote
> foo:foo

(lambda ((foo:arg1 foo:arg2)) (setq foo:myvar foo:arg1) (setq foo:main-var



  foo:arg2))


Even if template had "MAIN:main-var".



It looks like this has to do with translateCreateSymbol() in nl-symbol.c.



I want to be able to create a function in a new context, while allowing it to access symbols from other context.  



Is there a way?

Lutz

#1
Yes, there is a function called 'def-new' for that specific purpose, but the template must reside in its own context:


(context 'template)

(define (function (arg1 arg2))
  (setq myvar arg1)
  (setq MAIN:main-var arg2))

(context 'MAIN)

Now you can do:
(def-new 'template:function 'ctx:function)

You don't even have to use the same name:


(def-new 'template:function 'ctx:foo)

would work too.



'def-new' makes new functions in a different context from a template. In your case you also could just duplicate the whole context and have the same result using the function 'new' to make a whole context from a template:


(new 'template 'myfoo)

This would give you a 'myfoo:function' which looks just like the original 'template:function'



And here the last trick: after having done all this (probably interactively in the console) do:


(save "mysession")

and look into the file "mysession" it contains all your new definitions from the tempate.



Lutz

frontera000

#2
Thanks.  It makes sense.  



However, my template does not belong to the context being created yet.  The template itself is in another context.  And by calling a "creating" function I want to be able to create a new context on the fly with the function in that new context.



Is that possible?



I am not concocting this situation just for fun. It's just that that is the most sensible way for some of the work I am doing.



I guess if I understand you correctly, it is possible to have a separate context for just holding the template?   If that is so, then I may be able to get by with that -- and I think it is a great way actually.



Let me try that and come back.

Lutz

#3
QuoteThe template itself is in another context.


Yes, this is how it should be.


Quote...I want to be able to create a new context on the fly with the function in that new context. Is that possible?


Yes it is possible, that is what 'def-new' and 'new' are made for. 'new' for an entire context, 'def-new' for just one function: sometimes you want to do object mixins composing new contexts from different templates (John Small suggested this, where are you?).


QuoteI guess if I understand you correctly, it is possible to have a separate context for just holding the template?


Yes, this is how it should be done. The internal overhead of contexts is minimal, create as much contexts as you want (millions). They are just sub-branches in a symbol tree.



Lutz