Using objects in objects...

Started by oofoe, September 28, 2005, 12:26:13 PM

Previous topic - Next topic

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](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)[/code]

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


Radius of s is 1.

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


Thanks in advance!



Jos'h[/color]
Testing can show the presence of bugs, but not their absence.

Lutz

#1
Here is a working code:



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



(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

#2
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
Testing can show the presence of bugs, but not their absence.

Lutz

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