I don't understand contexts... :-)

Started by cormullion, January 14, 2006, 06:59:11 AM

Previous topic - Next topic

cormullion

How do I get this function to return the value of a symbol in another context? I'm' trying to construct the reference:



(context 'MyContext)
(set 's "hi there")

(context MAIN)
(define (func ctx w)
(println "inside function, context is " ctx " and symbol value is " w)
(println "inside function " ctx:w))

(println "MAIN:s is " s)
;- MAIN:s is nil

(println "MyContext:s is " MyContext:s)
;- MyContext:s is hi there

(println  (func MyContext s))

; inside function, context is MyContext and symbol value is nil ?
; inside function nil ?

Lutz

#1
Here is a changed working version and some comments:



(context 'MyContext)
(set 's "hi there")
(context MAIN)

(define (func ctx w)
   (println "inside function, context is " ctx " and symbol value of w is " w)  

   (println "inside function " (eval (sym (name w) ctx)))
   ; or in versions after 8.7.1 you also can do
   ;(println "inside function " (context ctx (name w)))
)

(println "MAIN:s is " s) ; sin MAIN hasn't been set yet so it is nil

(println "MyContext:s is " MyContext:s)

(println  (func MyContext 's)) ; s has to be passed as a symbol, else nil gets passed


The main problem was the function call (fun ....) the s has to be passed as a symbol.



The other problem was in the line:

(println "inside function " ctx:w)

Only ctx can be a variable holding MyContext. The w will always  be w, so from ctx:w you get MyContext:w but not MyContext:s



Lutz



ps: perhaps this should be posted in the last  newLISP topic-group?

cormullion

#2
Thanks Lutz. I hadn't appreciated the point of :


(context ctx (name w))


before, and now the mists are starting to evaporate.



ps: Yes, I've been posting in the wrong place again - sorry!