what's wrong with this ?

Started by Maurizio, March 08, 2006, 09:03:49 AM

Previous topic - Next topic

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

Lutz

#1
A context symbol should always belong to MAIN, change to:



(context 'SECOND)
(define (init)
    (new CTX 'MAIN:asymbol))

now it works:

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

Maurizio

#2
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






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

Lutz

#3
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

Maurizio

#4
Thanks, very clear explanation

Maurizio