Accessing the meaning of a word inside an object
In the interpreter:
>> qurl: "1234"
== "1234"
>> R: make object! [probe qurl qq: qurl probe qq]
"1234"
"1234"
== make object! [
qq: "1234"
]
This behaves as I expected. All "variables" or "words" are global by default.
Using script:
REBOL []
qurl: "1234"
Q: make object! [
probe join "[Q before qq] qurl: " qurl
qq: qurl
probe join "[Q] qq: " qq
qurl: qurl
probe join "[Q after qurl] qurl: " qurl
]
probe join "[main before Q] qurl: " qurl
Q
probe join "[main after Q] qurl: " qurl
returns:
"[Q before qq] qurl: none"
"[Q] qq: none"
"[Q after qurl] qurl: none"
"[main before Q] qurl: 1234"
"[main after Q] qurl: 1234"
I expect everyone probe
inside to Q
object!
return "1234"
, but no one will.
Why is this?
source to share
When Rebol creates an object, it first collects (set-) words from the spec and uses them to create a new object. Initially, the words of the new object are assigned none
. The BOM is then linked to the new entity and evaluated.
In your first example, you did not include qurl:
in your spec, so you are not part of your object. Instead, you get "global" qurl
. In the second example, it qurl
is part of your object and is initiated with none
.
Your options for accessing the value associated with the global qurl
in the second example:
1. Build the block so that when the block has been evaluated, it directly accesses this value:
Q: make object! compose [
qq: (qurl)
qurl: (qurl)
]
2. Access the word from the global context:
Q: make object! [
qq: system/words/qurl ; Rebol 2
qq: system/contexts/user/qurl ; Rebol 3
]
source to share