It took me a while to recognize that string literals in REBOL don't
mean what they do in other languages.  In another language (Perl, for
example) the statement

    $a = "";

is translated into instructions that ask the interpreter to create a
new, empty string, then store a reference to that string in $a.  By
contrast, a string literal in the source text is simply translated
into a reference to a string which is initialized with the literal's
value.

The result of translating the REBOL expression

    b: [a: ""]

can be diagrammed as

    b -> {{block! 1 *}}
                    |
                    V
                    <<a: *>>
                         |
                         V
                         {{string! 1 *}}
                                     |
                                     V
                                     <<>>

Evaluating the expression

    do b

modifies the state of affairs to

    b -> {{block! 1 *}}
                    |
                    V
                    <<a: *>>
                         |
                         V
    a -----------------> {{string! 1 *}}
                                     |
                                     V
                                     <<>>

so that a now refers to the same string as b/2.  Modifying the content
of that string has a side-effect visible to any reference to that
string.

    >> b: [a: ""]
    == [a: ""]
    >> do b
    == ""
    >> append a "YOW!"
    == "YOW!"
    >> b
    == [a: "YOW!"]
    >> insert second b "HOO-"
    == "YOW!"
    >> a
    == "HOO-YOW!"
    >> b
    == [a: "HOO-YOW!"]

So string literals in REBOL are just initilizers for anonymous strings.

-jn-

Reply via email to