[REBOL] objects without overhead Re:
To create only one instance of the function, define the function first and assign it to a word. Then, assign the object element to a reference of this word. Example: my-func: func [][print "hello!"] obj1: make object! [f: :my-func] obj2: make object! [f: :my-func] same? (get in obj1 'f) (get in obj2 'f) == true - Michael Jelinek [EMAIL PROTECTED] on 10/17/2000 04:44:00 PM From: [EMAIL PROTECTED] on 10/17/2000 04:44 PM Please respond to [EMAIL PROTECTED] To: [EMAIL PROTECTED] cc: Subject: [REBOL] objects without overhead 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.
[REBOL] objects without overhead Re:(4)
Once an object is made you can use "make (ancestor oject)!" on that new object to create your new objects. At least if I remember correctly from the RTOG. Paul Tretter -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Wednesday, October 18, 2000 7:23 AM To: [EMAIL PROTECTED] Subject: [REBOL] objects without overhead Re:(3) Hi, Rishi, [EMAIL PROTECTED] wrote: > > state: make object! [ > new: func [ > puzzle [block!] > ][ > > return make object! [ > puz: copy reduce puzzle > get-puzzle: :get_puzz > ] > ] > get_puzz: does [return copy puz] > test-obj: new [1 2 3 4 0 5 6 7 8] > print test-obj/get-puzzle > ] > [snip] > > I don't understand why get-puzzle cannot access word 'puz. > Look at the following (awful ASCII art) diagram, which shows the contexts and references of the various names involved: (global) | +--...--+ | | state==>(object!) (etc) | ++---+ || | new |test-obj==>(object!) || | puzzle |+--+ || | get_puzz<==get-puzzle puz As the above code is being evaluated, look at the contexts that are established, and the words they contain. Context for...Contains words - (REBOL global)'state (along with whatever else was there) STATE 'new 'get_puzz 'test-obj (the object's "members") NEW 'puzzle (the function's argument) Now, the next-to-last line inside STATE makes 'test-obj (in the context of STATE) refer to a newly-created object which has the following context: TEST-OBJ 'get-puzzle 'puz Here's the critical issue: TEST-OBJ refers to an object whose component GET-PUZZLE refers to the *value* of GET_PUZZ, which contains words defined *in the context of its own definition* and in that context, *there is no 'puz*. Remember that 'puz is defined in the context of the object to which TEST-OBJ now refers, but *not* in the context of STATE. If you'll pardon the anthropomorphism, TEST-OBJ knows both GET-PUZZLE and PUZ, and introduced GET-PUZZLE to GET_PUZZ, but nobody ever introduced GET_PUZZ to PUZ. What you were trying to do would only have succeeded if REBOL used "dynamic scoping" such as Lisp or xBase use, in which new variables are added to the global "environment" and are visible to everyone after (chronologically!!!) the point of definition. REBOL contexts don't behave that way. Hope this helps! -jn-
[REBOL] objects without overhead Re:(3)
Hi, Rishi, [EMAIL PROTECTED] wrote: > > state: make object! [ > new: func [ > puzzle [block!] > ][ > > return make object! [ > puz: copy reduce puzzle > get-puzzle: :get_puzz > ] > ] > get_puzz: does [return copy puz] > test-obj: new [1 2 3 4 0 5 6 7 8] > print test-obj/get-puzzle > ] > [snip] > > I don't understand why get-puzzle cannot access word 'puz. > Look at the following (awful ASCII art) diagram, which shows the contexts and references of the various names involved: (global) | +--...--+ | | state==>(object!) (etc) | ++---+ || | new |test-obj==>(object!) || | puzzle |+--+ || | get_puzz<==get-puzzle puz As the above code is being evaluated, look at the contexts that are established, and the words they contain. Context for...Contains words - (REBOL global)'state (along with whatever else was there) STATE 'new 'get_puzz 'test-obj (the object's "members") NEW 'puzzle (the function's argument) Now, the next-to-last line inside STATE makes 'test-obj (in the context of STATE) refer to a newly-created object which has the following context: TEST-OBJ 'get-puzzle 'puz Here's the critical issue: TEST-OBJ refers to an object whose component GET-PUZZLE refers to the *value* of GET_PUZZ, which contains words defined *in the context of its own definition* and in that context, *there is no 'puz*. Remember that 'puz is defined in the context of the object to which TEST-OBJ now refers, but *not* in the context of STATE. If you'll pardon the anthropomorphism, TEST-OBJ knows both GET-PUZZLE and PUZ, and introduced GET-PUZZLE to GET_PUZZ, but nobody ever introduced GET_PUZZ to PUZ. What you were trying to do would only have succeeded if REBOL used "dynamic scoping" such as Lisp or xBase use, in which new variables are added to the global "environment" and are visible to everyone after (chronologically!!!) the point of definition. REBOL contexts don't behave that way. Hope this helps! -jn-
[REBOL] objects without overhead Re:(3)
Rishi wrote:
> I was really hoping this would work. It seemed like an elegant solution.
>> state: make object! [
[new: func [
[puzzle [block!]
[][
[make object! [
[puz: copy reduce puzzle
[get-puzzle: :get_puzz
[]
[]
[get_puzz: does [return copy puz]
[]
>> test-obj: state/new [1 2 3 4 0 5 6 7 8]
>> probe test-obj
make object! [
puz: [1 2 3 4 0 5 6 7 8]
get-puzzle: func [][return copy puz]
]
>> test-obj/get-puzzle
** Script Error: puz has no value.
** Where: return copy puz
> I don't understand why get-puzzle cannot access word 'puz.
state/get_puzz is not in the same context as test-obj/puz.
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
-><-
- Original Message -
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, 18 October 2000 3:04 PM
Subject: [REBOL] objects without overhead Re:(2)
> There were a few mistakes in my previous emails code. I tried out the
basic concept though...but it was a failure. here's a simplified version of
code. This is real code and I have run it through rebol:
>
>
> ; CODE BEGINS HERE
> state: make object! [
> new: func [
> puzzle [block!]
> ][
> return make object! [
> puz: copy reduce puzzle
> get-puzzle: :get_puzz
> ]
> ]
> get_puzz: does [return copy puz]
>
> ;;;: TEST CODE
> ; comment {
> test-obj: new [1 2 3 4 0 5 6 7 8]
> print test-obj/get-puzzle
> ; }
>
> ]
> ; CODE ENDS HERE
>
> Now if you copy this code and run it, you get the following error:
>
> ** Script Error: puz has no value.
> ** Where: return copy puz
> >>
>
> I was really hoping this would work. It seemed like an elegant solution. I
don't understand why get-puzzle cannot access word 'puz.
>
>
> Previously, you ([EMAIL PROTECTED]) wrote:
> > 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 ([EMAIL PROTECTED]) 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.
> > >
> > >
> >
> >
>
[REBOL] objects without overhead Re:(2)
There were a few mistakes in my previous emails code. I tried out the basic concept
though...but it was a failure. here's a simplified version of code. This is real code
and I have run it through rebol:
; CODE BEGINS HERE
state: make object! [
new: func [
puzzle [block!]
][
return make object! [
puz: copy reduce puzzle
get-puzzle: :get_puzz
]
]
get_puzz: does [return copy puz]
;;;: TEST CODE
; comment {
test-obj: new [1 2 3 4 0 5 6 7 8]
print test-obj/get-puzzle
; }
]
; CODE ENDS HERE
Now if you copy this code and run it, you get the following error:
** Script Error: puz has no value.
** Where: return copy puz
>>
I was really hoping this would work. It seemed like an elegant solution. I don't
understand why get-puzzle cannot access word 'puz.
Previously, you ([EMAIL PROTECTED]) wrote:
> 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 ([EMAIL PROTECTED]) 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.
> >
> >
>
>
[REBOL] objects without overhead Re:(2)
You could try something like this: Outer: make object! [ Inner: make object! [ Data1: string! Data2: integer! ] Data1: func [Inner [object!]] [ Inner/Data1 ] Data2: func [Inner [object!]] [ Inner/Data2 ] Make-Inner: func [Block [block!]] [ clone Inner Block ] set 'Dialect func [Block [block!]] [ make object! bind Block 'self ] ] Dialect [ Zot: Make-Inner [ Data1: "one" Data2: 2 ] Zot2: Make-Inner [ Data1: "11" Data2: 22 ] print Data1 Zot print Data2 Zot print Data1 Zot2 print Data2 Zot2 ] Which, after pasting into the console, gives: one 2 11 22 Andrew Martin In, out and between Rebolutionary... ICQ: 26227169 http://members.nbci.com/AndrewMartin/ -><-
[REBOL] objects without overhead Re:
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 ([EMAIL PROTECTED]) 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. > >
