Using objects in objects...

For the Compleat Fan
Locked
oofoe
Posts: 61
Joined: Wed Sep 28, 2005 7:13 pm
Contact:

Using objects in objects...

Post by oofoe »

Hi,

I'm trying to use an object inside a context. I can create objects in the MAIN context fine, but when I try to do the same thing inside another context, the compiler bails. Can anyone suggest what's going wrong?

Here's a snippet of source code:

Code: Select all

(context 'sphere)

(setq centre '(0 0 0))
(setq radius 1)


(context 'render)

(define (go)
  (new 'sphere s)
  (setq s:radius 2)
  (print "Rendering a sphere of radius " s:radius ".\n")
)


(context 'MAIN)

(new sphere 's)
(print "Radius of s is " s:radius ".\n")

(new render 'r)
(r:go)

(exit)
Here's what a run looks like (Linux and Windows):

Code: Select all

Radius of s is 1.

context expected in function new : nil
called from user defined function go
Thanks in advance!

Jos'h[/color]

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

Post by Lutz »

Here is a working code:

Code: Select all

(context 'sphere) 

(setq centre '(0 0 0))
(setq radius 1)        

(context 'render)

(define (go)
  (new 'sphere 'MAIN:s)
  (setq s:radius 2)
  (print "Rendering a sphere of radius " s:radius ".\n")
)            

(context 'MAIN)   

(new sphere 's)     
(print "Radius of s is " s:radius ".\n")                 

(new render 'r) 
(r:go) 

(exit) 
s has to be a symbol and has to be part of MAIN. There is no such thing in newLISP as a context/object inside a context/object. All context symbols are and have to be are part of the MAIN context.

The code works this way but it would be better to end each context declaration with (context MAIN):

Code: Select all

(context 'sphere) 

(setq centre '(0 0 0))
(setq radius 1)        

(context 'MAIN) 

(context 'render)

(define (go)
  (new 'sphere 'MAIN:s)
  (setq s:radius 2)
  (print "Rendering a sphere of radius " s:radius ".\n")
)            

(context 'MAIN)   

(new sphere 's)     
(print "Radius of s is " s:radius ".\n")                 

(new render 'r) 
(r:go) 

(exit) 
If not tou have the symbol 'render' twice onec in MAIN as a context and as a member symbol sphere:render, which could bring problems in other situations.

Lutz

oofoe
Posts: 61
Joined: Wed Sep 28, 2005 7:13 pm
Contact:

Thanks!

Post by oofoe »

Whoa! I don't think would have *ever* figured that one out... Thanks very much for the explanation.

However, while it works now, this is somewhat inconvenient. It is very useful to be able to break apart the problems using objects inside of objects. Is there any other sort of namespace partitioning thing I could use for this, or is there some "Lisp-y" way that I should program that avoids this situation?

Thanks again!

Jos'h

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

Post by Lutz »

newLISP's OO model is very simple, and does not have a lot of facilities found in OO languages like Java or C++. Still you can do OO programmming in a flat object space, and copy or merge objects from other objects with 'new' and 'def-new'. See the examples in the manual.

Most people/projects use newLISP's contexts as namespaces to organize their programs and offer lexical isoloation of submodules. This is important when workiong in programming teams. newLISP's main programming model is functional not OO.

Lutz

Ps: very welcome to the group

Locked