Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: objects without overhead Re:

From: lmecir:geocities at: 24-Oct-2000 13:21

Hi Rishi, as an inspiration, I am sending you my old class.r Rebol [ Title: "Class" File: %class.r Author: "Ladislav Mecir" Email: [lmecir--geocities--com] Purpose: { A class hierarchy in Rebol } Category: [Advanced] ] throw-on-error: func [[throw] blk [block!] /local result] [ if error? set/any 'blk try blk [ throw :blk ] get/any 'blk ] ; method creator meth: func [ {defines a user method with given spec and body} [catch] spec [block!] body [block!] /local result ] [ ; try the consistence throw-on-error [ func spec copy/deep body ] func [self] reduce [ :func :spec 'bind/copy :body :in 'self to lit-word! 'self ] ] ; method calling dialect: !: func [ {method caller} [catch] message [block!] { The structure of a message is as follows: method-path object additional arguments according to the method spec } /local method-path method-name self position cls result ] [ method-path: throw-on-error [first :message] if not path? get/any 'method-path [ method-path: throw-on-error [ to path! get/any 'method-path ] ] method-name: first :method-path position: next message result: throw-on-error [do/next position] set/any [self position] :result method-name: throw-on-error [ in get get in :self 'class method-name ] if not method-name [ throw make error! {no such method} ] method-name: get method-name method-name: method-name self change :method-path 'method-name do compose [(:method-path) (position)] ] ; sample class sample-class: make object! [ ; a method returning the object type type: meth [] [ class ] ; a static value - counter counter: 0 ; a counting method count: meth [] [ counter: counter + 1 ] reftest: meth [/ref] [ either ref [ print "Ref" ] [ print "No ref" ] ] num: meth [] [number] addnum: meth [other] [(! [num other]) + (! [num self])] ] ; sample objects of a sample-class sample-object1: make object! [ class: 'sample-class number: ! [count self] ] probe sample-object1 sample-object2: make sample-object1 [ number: ! [count self] ] probe sample-object2 ; sample method calls probe ! [type sample-object1] probe ! [type sample-object2] ! [reftest sample-object2] ! [reftest/ref sample-object2] probe ! [num sample-object1] probe ! [num sample-object2] probe ! [addnum sample-object1 sample-object2] probe ! [addnum sample-object2 sample-object1] Regards Ladislav
> Well...I thought about this a bit and I THINK I found an elegant solution.
So I guess I'll respond to my own question...
> I think the solution to this problem is inner objects. Here is an example: > > myobject: make object! [ > - function1: func[][..] > - function2: func[][..] > - . > - . > - make: func[var1 var2 var3][ > - return make object! [ > - instancevar1: var1 > - instancevar2: var2 > - instancevar3: var3 > - > - instancefunc1: function1 ;copies a reference to function1 above... > - instancefunc2: function2 > - ] > - ] > ] > > ;;;create instance of object > coolobj: myobject/make > coolobj/instancfunc1 ; all instances access same function! less overhead
if many objects!!!
> . > . > . > I am not sure if this would work since I have never tested it (yet). The
questions in my mind are:
> 1. are functions copied by reference or value? if by value, this would not
solve my problem
> 2. do inner objects have access to outer object words? for example, can
'instancefunc1 access 'function1 as shown above?
> I hope this works. If it does, it would be a very simple but elegant
solution to my problem. It would be even more elegent when modules come around so I can keep all private words in context of myobject rather than the actual object returned...making code very clear.
> Rishi > > Previously, you ([rishi--picostar--com]) wrote: > > I am working on a program which requires about 1000 or so instances of
objects. It doesn't have to be OO but I like the design better that way. I am using a function as constructor like the following example (not real code..):
> > > > ;I used dashes to keep formatting intact (hopefully this works) > > > > make_myobject: func[var1 var2 var3][ > > --return make object! > > ----instancevar1: var1 > > ----instancevar2: var2 > > ----instancevar3: var3 > > > > ----instancefunc1: func[][...] > > ----------------. > > ----------------. > > ----------------. > > -] > > ] > > > > > > The thing I don't like about this solution is that every instance of
this object has duplicate copies of the functions (As far as I know...please correct me if I am wrong). This seems like a waste to me - especially if the object has 50 or so functions. Is there any OO way in Rebol to create multiple instances of this object without creating multiple copies of the functions associated with each object? I know how to do this in a non-object oriented fashion but would like to see an OO solution.