Sending a more commented version of Methods.
Have fun.
Ladislav
Rebol[
Title: "Methods"
Author: Ladislav Mecir
Email: [EMAIL PROTECTED]
Date: 1/12/1999
Version: 2.0.0
Purpose: "Code to prevent method code copying."
Comment:
It works as follows:
if an object ANOBJECT contains a function attribute FNC,
then if you create a copy of it with:
OTHEROBJECT: MAKE ANOBJECT [],
the code of FNC gets copied in Rebol, because OTHEROBJECT/FNC
must have access to the OTHEROBJECT's attributes, so Rebol
BIND's the OTHEROBJECT/FNC's code to the OTHEROBJECT's context
Rebol works differently, if object's attribute is of type OBJECT!.
In that case it does not copy the attribute, but instead it uses the
same
object (to preserve the memory?).
When we prevented the method code copying,
we must make sure the method has the access
to the object's attributes it operates on.
But that is easy - see later...
}
]
;Define the object like this:
example: make object! [
num: 1
;Other data attributes here...
;Now define the methods, common for all descendants.
;These are created only once
;and aren't copied,
;which means that there is only one copy of them in the computer memory.
methods: make object! [
;An example method
ex-method: func [
;every method must have access
;to object to which it has been sent
self [object!]
;the room for other method parameters...
param
] [
;This is the body of the method
self/num
]
;Other methods here...
]
]
;Function to call methods
!: func [self [object!] 'method [word!] param] [
do in self/methods method self param
]
;Now you can use the code like this:
>> ! example ex-method []
== 1
>> b: make example [num: num + 1]
>> ! b ex-method []
== 2
>> no-copying: same? get in example 'methods get in b 'methods
== true
>> no-copying: same? get in example/methods 'ex-method get in b/methods
'ex-method
== true