[REBOL] Obscure? You be the judge!
From: joel:neely:fedex at: 13-May-2002 23:17
I don't have a good explanation for this bit of surprising behavior.
All donations of wisdom gladly accepted.
Here's a odd-looking little object:
bug?: make object! [
width: 3
data: [1]
plop: func [b [block!]] [append data b]
child-proto: make object! [
label: "no such"
dataref: data
dump: func [] [print [label newline tab mold dataref]]
reset: func [] [dataref: head dataref]
]
children: make block! width
repeat i width [
append children make child-proto [
label: join "Child-" i
]
]
fizz: func [] [
foreach child children [
child/dump
]
]
]
It contains a data store (called DATA surprisingly enough) and
a block of subordinate objects that need to share access to DATA.
Within each child object, DATAREF is supposed to refer to the
same series as DATA. That way, after BUG?/PLOP is used to add
entries to DATA, the DUMP routine of each child should show the
additions. Let's try it:
>> bug?/fizz
Child-1
[1]
Child-2
[1]
Child-3
[1]
>> bug?/plop [3 5 7]
== [1 3 5 7]
>> bug?/fizz
Child-1
[1]
Child-2
[1]
Child-3
[1]
>>
Wozzattt?? It appears that each child's DATAREF has become a
completely independent block!? Howesomever...
If we add one line to the initialization of CHILDREN (inside
the REPEAT about a dozen lines in) like so:
bug?: make object! [
width: 3
data: [1]
plop: func [b [block!]] [append data b]
child-proto: make object! [
label: "no such"
dataref: data
dump: func [] [print [label newline tab mold dataref]]
reset: func [] [dataref: head dataref]
]
children: make block! width
repeat i width [
append children make child-proto [
label: join "Child-" i
dataref: data ;;; this line added
]
]
fizz: func [] [
foreach child children [
child/dump
]
]
]
... we now get the expected behavior!
>> bug?/fizz
Child-1
[1]
Child-2
[1]
Child-3
[1]
>> bug?/plop [3 5 7]
== [1 3 5 7]
>> bug?/fizz
Child-1
[1 3 5 7]
Child-2
[1 3 5 7]
Child-3
[1 3 5 7]
>>
As tempted as I am to sing
"Plop, plop, fizz, fizz,
Oh what a relief it is!"
I'm not feeling relieved. Why does the second version behave
differently? Both are initializing DATAREF to DATA, just at
different times, so I'm a bit puzzled.
Do I need more coffee?
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]