SET, REVISITED

A more precise statement is that evaluating a block containing a
set-word (remember that a balanced entry into the REBOL console is
treated as if contained by an implicit block) associates the word
with the value of the expression following it.  After evaluating:

    a: "1234"
    x: [a: "987"]

we have

    a -> {{string! 1 *}}
                     |
                     V
                     <<#"1" #"2" #"3" #"4">>

    x -> {{block! 1 *}}
                    |
                    V
                    <<a: *>>
                         |
                         V
                         {{string! 1 *}}
                                     |
                                     V
                                     <<#"9" #"8" #"7">>

(There is undoubtedly more "under the hood" for a set-word, but this
essay is about series, so the details of set-word are ignored in the
above diagram.)

After evaluating

    do x

the situation is different (and fairly distinctive to REBOL):

    a -------------------------+
                               |
                               |
    x -> {{block! 1 *}}        |
                    |          /
                    V         /
                    <<a: *>> /
                          \ /
                           V
                           {{string! 1 *}}
                                       |
                                       V
                                       <<#"9" #"8" #"7">>

Note that a now refers to the same string as referred to by the
second element of x's value.  This is different from setting a to
refer to a COPY of that "string literal", which does NOT happen in
REBOL.  To achieve this last effect, x would have to be set with:

   x: [a: copy "987"]

Therefore, if a is passed to an operation (such as 'append or
'insert) which actually modifies the value of its character
sequence, the modification will show up via x as well.  The console
transcript bears out these predictions:

    >> a: "1234"
    == "1234"
    >> x: [a: "987"]
    == [a: "987"]
    >> do x
    == "987"
    >> a
    == "987"
    >> append a "!!!"
    == "987!!!"
    >> x
    == [a: "987!!!"]

Since a and  second x  refer to the same string, this trick works in
the other direction as well.

    >> insert second x "???"
    == "987!!!"
    >> x
    == [a: "???987!!!"]
    >> a
    == "???987!!!"

(continued in essay/6)

Reply via email to