Contexts and global variables

Started by cameyo, April 30, 2019, 02:02:25 AM

Previous topic - Next topic

cameyo

I have the following problem:
; create two contexts (A1, A2)
> (context 'A1)
;-> A1
A1> (context MAIN)
;-> MAIN
> (context 'A2)
;-> A2
A2> (context MAIN)
;-> MAIN
; create a variable "a"
> (setq a 2)
;-> 2
> (context 'A1)
;-> A1
; context A1 do not see the variable "a"
A1> a
;-> nil
; context A1 do not see the variable "a"
A1> (context 'A2)
;-> A2
A2> a
;-> nil
A2> (context MAIN)
;-> MAIN
> a
;-> 2

; set variabile "a" to global
(global 'a)

; but A1 and A2 do not see the variable "a"
> (context 'A1)
;-> A1
A1> a
;-> nil
A1> (context 'A2)
;-> A2
A2> a
;-> nil

But this works (do not check variable value on contexts before globalize it:

A2> (context MAIN)
;-> MAIN
> (context 'B2)
;-> B2
B2> (context MAIN)
;-> MAIN
> (context 'B1)
;-> B1
B1> (context MAIN)
;-> MAIN
> (setq b 3)
;-> 3
> (global 'b)
;-> b
> (context B1)
;-> B1
B1> b
;-> 3
B1> a
;-> 2
B1> (context B2)
;-> B2
B2> b
;-> 3
B2> a
;-> 2
B2>

B2> (context MAIN)
MAIN
; the context A1 see only "b" ("a" is nil)
> (context A1)
A1
A1> a
nil
A1> b
3
A1>

; change variable value
A1> (context MAIN)
MAIN
> (setq a 10)
10
> a
10
> (context B1)
B1
B1> a
10
B1> (context A1)
A1
A1> a
nil
A1>

Why A1 and A2 do not see the variable "a" ?

what am i doing wrong?

Is there a rule to define global variables and contexts?

Lutz

#1
See here: http://www.newlisp.org/downloads/newlisp_manual.html#context_rules">http://www.newlisp.org/downloads/newlis ... text_rules">http://www.newlisp.org/downloads/newlisp_manual.html#context_rules  



especially paragraph 3.

cameyo

#2
Thanks Lutz.
> (setq a 1)
;-> 1
> (context 'A1)
;-> A1
A1> (symbols)
;-> ()
A1> a
;-> nil
A1> (symbols)
;-> (a)