[EMAIL PROTECTED] wrote:
>
> can't resist to show you a few examples of Rebol code (just for fun, don't
> be afraid).
>
> Ex1:
>
> f: func [/local a][
> do func [] [
> a: ""
> ]
> insert a "1"
> ]
>
> Results:
> >> f
> == ""
> >> f
> == ""
>
> interesting, isn't it?
>
Invoking 'f doesn't accumulate a string of ones for the same
reason that
fc: func [/local a] [a: copy "" insert a "1"]
doesn't (although it's a bit more camoflaged!) For some
(undocumented, but undoubtedly fascinating) reason, 'func makes a
deep copy of the block that serves as the function's body.
>> body: [a: "" insert a "1"]
== [a: "" insert a "1"]
>> f: func [/local a] body
>> f
== ""
>> f
== "1"
>> f
== "11"
>> f
== "111"
>> body
== [a: "" insert a "1"]
>> second :f
== [a: "1111" insert a "1"]
In your example, evaluating the fragment
do func [] [a: ""]
defines an anonymous function, containing a COPY of the block
[a: ""]
and then immediately executes it. Since there is no reference to
that anonymous function, it effectively disappears (it is gobbled
up by the garbage collector at some point). Invoking 'f the next
time causes the anonymous function to be defined all over.
Since each function creation makes a deep copy of the body, there
is no reference back to the string initialized as "" in the body
of 'f.
The returned value of your function is the result from 'insert
(which always returns a series positioned AFTER the value inserted
in the argument series). To see the entire character sequence
referenced by a's string, try
>> f: func [/local a] [do func [] [a: ""] insert a "1"]
>> f
== ""
>> head f
== "1"
-jn-