set
[1/3] from: ryan::christiansen::intellisol::com at: 15-Mar-2001 16:49
Reading the REBOL/Core manual, I am still uncertain as to what 'set
accomplishes. I noticed these two functions in a recent post, one using
'set and the other not using 'set. What does 'set accomplish?
exec: func [series [series!]] [
if error? try [return do series] [
join exec_start [series exec_end]]]
set 'stringcompose func [
{Evaluates a string of expressions, only evaluating
executable parens, and returns a block.}
Text [String!]] [
clear buffer
parse/all Text rule
buffer]
Ryan C. Christiansen
Web Developer
Intellisol International
4733 Amber Valley Parkway
Fargo, ND 58104
701-235-3390 ext. 6671
FAX: 701-235-9940
http://www.intellisol.com
Global Leader in People Performance Software
_____________________________________
Confidentiality Notice
This message may contain privileged and confidential information. If you
think, for any reason, that this message may have been addressed to you in
error, you must not disseminate, copy or take any action in reliance on it,
and we would ask you to notify us immediately by return email to
[ryan--christiansen--intellisol--com]
[2/3] from: larry:ecotope at: 15-Mar-2001 15:19
Hi Ryan
Here is a short answer, others may wish to expand. When issued in the global
context, both methods give the same results. But within the block defining
an object, they do not:
>> ob: make object! [ f1: func [x][x * x] set 'f2 func [x][x + x]]
>> probe ob
make object! [
f1: func [x][x * x]
]
; notice that only f1 is defined within the object, f2 is global
>> f2 4
== 8
>> ob/f1 4
== 16
>>
Using a setword causes the function to be defined within the object. Using
the 'set function does not, instead, the word naming the function will be
defined in the first ancestor context where it has already been defined, or
if the word is previously undefined, in the global context.
This behavior of 'set is actually very useful for exporting functions from
objects, while in the func definition all of the object vars are available
to it.
HTH
-Larry
[3/3] from: brett:codeconscious at: 16-Mar-2001 10:27
Hi Ryan
A few examples, and explanations.
set sets a word to a value
>> set 'myword "test"
== "test"
>> myword
== "test"
>> word-to-set: 'test-word
== test-word
>> set word-to-set 9
== 9
>> test-word
== 9
>> set [w1 w2 w3] 4
== 4
>> w1
== 4
>> w2
== 4
>> w3
== 4
>> set [w1 w2 w3] [1 2 3]
== [1 2 3]
>> w1
== 1
>> w2
== 2
>> w3
== 3
The piece of code you snipped out was part of a larger context - an object.
>> an-object: make object! [
[ name-of-object: "brett"
[ function1: does [print ["name of object is" name-of-object]]
[ set 'function2 does [ print ["name of the same object"
name-of-object]]
[ ]
>> an-object/name-of-object
== "brett"
>> an-object/function1
name of object is brett
>> function2
name of the same object brett
You can see that function1 and function2 have the same access to the context
of the object but function2 is accessed in the global context instead of the
context of the object.
function1
is a field of the object that is set to a function that was
defined within the context of the object.
function2
is a word that is set in the global context to a function. This
function was defined within the context of the object.
So looking back at Marcus' stringcompose! object. The object keeps all the
parse rules and supporting functions in a nice tidy bundle. Then by using
set Marcus has create a globally available function that uses all the stuff
in the object. You don't even need to refer to the object again - it just
hangs around holding the bits together for the use of the stringcompose
function.
So in this case, it doesn't change the functionality of the program, it
neatens the structure of the progam.
HTH
Brett