what's wrong with this ?

Notices and updates
Locked
Maurizio
Posts: 52
Joined: Mon Jul 28, 2003 3:06 pm
Location: Italy

what's wrong with this ?

Post 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

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

Post 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

Maurizio
Posts: 52
Joined: Mon Jul 28, 2003 3:06 pm
Location: Italy

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

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

Post 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

Maurizio
Posts: 52
Joined: Mon Jul 28, 2003 3:06 pm
Location: Italy

Post by Maurizio »

Thanks, very clear explanation
Maurizio

Locked