Page 1 of 1
what's wrong with this ?
Posted: Wed Mar 08, 2006 5:03 pm
by Maurizio
calling sec:init i'm trying to create a new object, of type ctx,
in the object sec, but newlisp complains with the following error
symbol not in MAIN context in function new : asymbol
called from user defined function sec:init
this is the program :
(context 'CTX)
(define (doit)
(set 'x 1))
(context 'SECOND)
(define (init)
(new CTX 'asymbol))
(context 'MAIN)
(new SECOND 'sec)
(sec:init)
Any suggestion ?
Regards
Maurizio
Posted: Wed Mar 08, 2006 6:47 pm
by Lutz
A context symbol should always belong to MAIN, change to:
Code: Select all
(context 'SECOND)
(define (init)
(new CTX 'MAIN:asymbol))
now it works:
Code: Select all
newLISP v.8.8.0 on OSX UTF-8, execute 'newlisp -h' for more info.
> (new SECOND 'sec)
sec
> (sec:init)
asymbol
> (symbols asymbol)
(asymbol:SECOND asymbol:doit asymbol:x)
>
Also: unless CTX context is loaded from its own file, I would put a (context 'MAIN) when finishing the code of CTX, before doing (context 'SECOND) in the same file.
Lutz
Posted: Thu Mar 09, 2006 11:24 am
by Maurizio
However I don't understand the following example:
in context FIRST the x gets the correct value,
in context SECOND a pre-definition of x is neeed,
otherwise an error occurs.
Any suggestion ?
Regards
Maurizio
Code: Select all
(context 'FIRST)
(define (create aname val)
(new FIRST aname)
(set 'aname (eval aname))
(set 'aname:x val))
(define (printit)
(println "in first")
(println x)) ;; << ---- this works without pre-definitions
(context 'SECOND)
(set 'x nil) ;; <<----- this is needed
(define (create aname val)
(new SECOND aname)
(set 'aname (eval aname))
(set 'aname:x val))
(define (printit)
(println "in second")
(x:printit)) ;; <<----- this need a pre-definition
(context 'MAIN)
(FIRST:create 'afirst 1)
(SECOND:create 'asecond afirst)
(asecond:printit)
(exit)
Posted: Thu Mar 09, 2006 12:39 pm
by Lutz
There are two reasons for the error hen not having (x:printit):
(1) during loading of your program, when parsing the statement (x:printit), newLISP sees 'x' and has to decide if this is a local variable SECOND:x which will hold a context in the future, or if 'x' is a context by itself.
At this moment 'x' does not exists as a local variable of SECOND, so newLISP creates x in MAIN as a context, not as a local variable holding a context. Predefining 'x' with (set 'x nil) makes sure that newLISP understands 'x' as a local variable holding a context in thr future.
(2) during run-time when (new SECOND aname) is executed. The new context inside of variable aname whill be created without a local variable 'x' becuase SECOND does not have an 'x'. By predefining with (set 'x nil) you make sure that it exists when creating the new context.
Lutz
Posted: Thu Mar 09, 2006 1:24 pm
by Maurizio
Thanks, very clear explanation
Maurizio