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

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

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

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

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

Locked