Page 1 of 1

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

Posted: Sat Jan 14, 2006 2:59 pm
by cormullion
How do I get this function to return the value of a symbol in another context? I'm' trying to construct the reference:

Code: Select all

(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 ?

Posted: Sat Jan 14, 2006 4:38 pm
by Lutz
Here is a changed working version and some comments:

Code: Select all

(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:

Code: Select all

(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?

Posted: Mon Jan 16, 2006 8:44 am
by cormullion
Thanks Lutz. I hadn't appreciated the point of :

Code: Select all

(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!