[REBOL] Re: Need help on something tricky...
From: chrismorency:videotron:ca at: 4-Oct-2001 1:24
Hi Andrew,
> Would a member function like:
> New: does [make self []]
> be helpful?
Yes and no, unfortunately, the way "make" works under rebol is to clone
everything from properties to methods.
Try the following :
a: make object! [
p: "nice string"
m: func [] [return self/p]
]
b: make a []
>> probe a
== make object! [
p: "nice string"
m: func [] [return self/p]
]
>> probe b
== make object! [
p: "nice string"
m: func [] [return self/p]
]
now change the following:
a/p: "nasty string"
a/m: func [] [return "nope, this shouldn't be"]
probe a
probe b
>> probe a
== make object! [
p: "nasty string"
m: func [] [return "nope, this shouldn't be"]
]
>> probe b
== make object! [
p: "nice string"
m: func [] [return self/p]
]
You would expect p to remain "nice string" in b, but since m was inherited
from a, you would have expected m to have changed in b too... somehow, since
a was class for b... however class doesn't exist under rebol.
Now let's say you design something big, with let's say 100 objects based on
one class... you would get all properties into each object, which is good,
but you would also get all functions (methods)... now as you can see, RT did
it this way because it somehow make sense when you consider it's a scripting
language BUT you get all of these different objects using a lot of memory.
The way I've implemented my library may eventually be very bad-looking... it
may add a lot of overhead. However, it will use less memory.
In your example : does [return make self []]... it will actually return a
new object based on self. However, everything will be copied.
So far, I've implemented the following methods : make (class declaration),
make/from (class inheritance), new (object declaration) and type-of (object
vs class validation)... I'm about to implement super... after that I have to
test every possible datatype within an object. Class within class within
class will end up being object within object within object... this is all
through recursivity (how many time can I go recursive without having Rebol
hanging on me ?)
Btw, I noticed you were working on a dice roller on the mailing list, I
actually wrote something if you're interested. I always write a dice-roller
as my first software for all language I learn. In fact, the reasons I'm
writing this class-object thing is to build a rpg software.
Best,
Chris