[REBOL] Re: Scope? Any advice would be appreciated.
From: g:santilli:tiscalinet:it at: 28-Jan-2001 20:48
Hello [rebol--mascari--org]!
On 28-Gen-01, you wrote:
r> Everything somewhat makes sense. Everything is just an
r> "object" that maintains itself once declared. And code appears
r> to be self-modifying. Ok.
Yes, because code is nothing more than data.
r> However, I have a follow-up question. The following routine
r> *does* work:
[...]
I was expecting that question. :-)
This is a matter of immutable vs mutable values. You cannot change
an INTEGER! value in REBOL (in your function you are using the ADD
native to create a *new* integer value), but you can change the
elements of a BLOCK! value (you don't create a new BLOCK! value,
you just change one of its elements).
r> works as one would expect. However, the same concept applied
r> to a block, b: [ 0 0 0 ] as in the earlier example, does not
r> work. Instead I have to make a copy of [ 0 0 0 ] - b: copy [ 0
r> 0 0 ].
You need to create a new fresh block each time only if you really
need it (that is, if you're going to change it). Doing this
automatically for each block wouldn't be a good idea, because most
of the time blocks are not changed (think about code blocks).
r> I am not clear as to why I would need to make a copy - why
r> wouldn't the b: [ 0 0 0 ] just blow away the current contents,
The matter is not the assignments itself, but is what you are
assigning. As you see, you changed that very block in the
function, so the second call looks like:
b: [0 0 1]
and not:
b: [0 0 0]
r> Gabriele, he showed what appears to me to be behavior of self
r> modifying code. However, I still don't understand why using
r> "copy" forces the code to update, whereas the normal
r> assignment operator does not.
Because then 'b will refer to a copy of that block in the code,
NOT that very block in the code. If you change the copy, the
original remains the same.
r> Lastly, since the code appears to be self-modifying, and
r> behavior of assignments change depdending upon whether it is
r> the first call or a subsequent one, I am curious as to what
r> advantages this has vs the confusion that it will cause people
r> coming from other programming environments.
It's a bit confusing in the beginning, but you'll find it obvious
and natural when you'll have some more experience with REBOL.
To be better, you have to be different. :-)
Regards,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/