[REBOL] Re: Object problem
From: joel:neely:fedex at: 18-May-2001 7:12
Richard Smolak wrote:
> o: make object! [
> a: 5
> x: [a]
> ]
>
...
> o: make o [b: 10]
> append o/x 'b
> == [a b]
> do o/x
> ** Script Error: b has no value
> ** Where: do-events
> ** Near: b
> >>
>
I won't repeat the answer that Frank already provided, but
take the opportunity to make a sales pitch for our mental
model of REBOL to separate more carefully between source
notation and "inner REBOL". (I've been bitten myself, and
have recently found this distinction helpful.)
We can have multiple words whose names are spelled the same,
if those words "live" in different contexts. That was the
key to the above puzzler. We can also have words whose
names are not spelled according to the standard REBOL rules,
if we construct those words ourselves instead of having
REBOL create them by the default DO or LOAD behavior.
A "source code word" must be constructed by certain rules
(letters, digits, certain punctuation marks, etc.) because
that's how REBOL decides at LOAD time to construct a WORD!
instead of (e.g.) an INTEGER! or a URL! or whatever. But
Ladislav has published some little koans that can be put
together in the following block with curious behavior:
>> b
== [+1 a a a a a]
>> print b
0.9 42 -1 wot? impossible!
>> length? b
== 5
>> foreach thingie b [print get thingie]
0.9
42
-1
wot?
impossible!
>> foreach thingie b [
[ print [type? :thingie tab type? get thingie]]
word decimal
word integer
word integer
word string
word string
>>
*** If you want to ponder on this puzzle, don't read
*** beyond this point!
The curious block B was created as follows:
>> a: 42
== 42
>> b: [a]
== [a]
>> use [a] [a: -1 append b [a]]
== [a a]
>> do func [a][append b [a]] "wot?"
== [a a a]
>> do compose/deep [(to-set-word "a a") "impossible!"
[ append b [(to-word "a a")]]
== [a a a a a]
>> do compose/deep [(to-set-word "+1") .9
[ insert head b [(to-word "+1")]]
== [a a a a a]
>> b
== [+1 a a a a a]
Consequently, the first and last "real words" in B have names
that wouldn't have been legal as "source code words". This
could provide some interesting food for thought in the area
of code shrouding!
Just another illustration of the subtlety available with
an extensible, dynamic language!
-jn-
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com