[REBOL] Re: The truth about scope
From: mokkel:gmx at: 13-Apr-2005 14:36
Hello Gabriele,
> the same value. (Almost, there's a detail I'm not going to reveal
> here, for the sake of simplicity.)
So what's the detail ? :-) You could reveal it in the answer to this post.
:-)
Otherwise:
Your answer is in some part what I tried to criticize a bit. For the
newcomer to Rebol, RT tries to give the impression it's just a normal
language --- but way more powerful ---- but the there is never a concise
description of what it is supposed to be that is more powerful (execpt
statements like dialecting and such). Over the time one can collect all
the little pieces from various pages and articles or Carls comments. But
what stays is that in normal dayly use the "kind of normal" lexical
scoping rules (and such things) dominate and people use it like this. I
understand if the first pages on the Rebol documentation would put the
theory first, some people would be scared or wouldn't like it anymore, but
it might prevent problems in the long run. And if Rebol is that much
easier there should be made an attempt at the beginning to explain this to
the people.
But thanks for your explanations.
I would like to hear you explanation of the persistence of blocks in
functions. :-)
Or maybe you can correct mine.
f: func [x /local bl] [
bl: []
append bl x
]
>> f "hello"
== ["hello"]
>> source f
f: func [x /local bl][
bl: ["hello"]
append bl x
]
Is it correct that:
Because it's only data anyway, the block after the "bl:" is the
representation of that particular block during interpretation of that
function. Thus if the function appends something to it, the block changes
and so its written representation.
On the other side when we copy the block before that (bl: copy []), the
interpreter encounters again that block, the written representation
doesn't change, its still for that block, but the now we got a runtime
object attached to the word 'bl and for this runtime-block we don't have
anymore a written representation and thus it has the same effect as it
would be created everytime new (or just on every function invocation we
have an empty block in 'bl).
So the point is that because of the evaluation of the function in the
first case bl holds a value we have some "persistent" representation for
(in the function code/data) and in the second it just exists at runtime
and if we would 'save the function it would be lost, unless we try to save
it explicitely. Like that:
f: func [x /local bp bl] [
bp: []
bl: copy []
append bl x
append/only bp bl
]
>> f "hello"
== [["hello"]]
>> f "you"
== [["hello"] ["you"]]
>> source f
f: func [x /local bp bl][
bp: [["hello"] ["you"]]
bl: copy []
append bl x
append/only bp bl
]
So is my model correct in this regard ?
Thanks again.
Michael