[REBOL] Re: Object private member (was: objects: overhead, private data, naming
From: joel::neely::fedex::com at: 20-Sep-2001 3:22
Hi, Christope,
It's the other way 'round!
CRS - Psy Sel/SPO, COUSSEMENT, Christophe, CPN
wrote:
> Hi REBOLians:
>
> About object private members, could anybody explain this
> to me:
>
...
> So... it looks like within an object, the use of 'set
> invoque a privatization of the word, which is not any more
> visible from the outside.
>
No. The use of SET affects the global context, rather than
the context of the object you're constructing.
>> o: context [
[ _a: 5
[ set '_b 10
[ get-a: does [print-a]
[ set 'print-a does [print _a]
[ set 'get-b does [print _b]
[ ]
>> get-b
10
> Great: I can use this property for making private members,
> but this way of doing is still strange to me.
>
Sorry. This can't be used to create private members.
> Any clue why a 'set-word! has two different ways of
> handling things, within this context ?
>
You'd have to get someone from RT (Holger? Comments, please?)
to explain the original motivation, but there are some easy-
to-notice consequences. Notice first that
>> source o
o:
make object! [
_a: 5
get-a: func [][print-a]
]
shows us that not everything done during the creation of an
object ends up *in* the object. This allows us to do
something like the following:
>> obj-count: 0
== 0
>> obj-spec: [x: 0 y: 0 set 'obj-count obj-count + 1]
== [x: 0 y: 0 set 'obj-count obj-count + 1]
;; now we can make a bunch of similar objects...
>> ob1: make object! obj-spec
>> ob2: make object! obj-spec
>> ob3: make object! obj-spec
;; ...and keep up with how many there are...
>> obj-count
== 3
;; ...without any overhead within the individual objects!
>> source ob1
ob1:
make object! [
x: 0
y: 0
]
The object specification is simply a block of REBOL that is
evaluated in a special way; set-words in the object spec cause
new words to be placed in the context under construction, but
everything else just does whatever it does normally. You can
take advantage of this fact in a variety of ways:
>> chatty-spec: [
[ print "Hi! I'm making another object"
[ set 'obnr obnr + 1
[ selfnr: obnr
[ print ["This new object is numbered" obnr]
[ set 'temp now/time
[ selfwhen: temp
[ print ["and was created at" temp]
[ print "I've finished now!"
[ ]
== [
print "Hi! I'm making another object"
set 'obnr obnr + 1
selfnr: obnr
print ["This new object is numbered"...
>> obnr: 0
== 0
(Notice that both OBNR and TEMP are globals, but only OBNR needs
to be initialized.)
>> o1: make object! chatty-spec
Hi! I'm making another object
This new object is numbered 1
and was created at 3:19:42
I've finished now!
>> o2: make object! chatty-spec
Hi! I'm making another object
This new object is numbered 2
and was created at 3:19:50
I've finished now!
>> o3: make object! chatty-spec
Hi! I'm making another object
This new object is numbered 3
and was created at 3:19:59
I've finished now!
>> source o1
o1:
make object! [
selfnr: 1
selfwhen: 3:19:42
]
HTH!
-jn-
--
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com