Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Scope? Any advice would be appreciated.

From: lmecir:mbox:vol:cz at: 28-Jan-2001 21:07

Hi Victor,
> Thanks to all that replied! > > Everything somewhat makes sense. Everything is just an "object" that > maintains itself once declared. And code appears to be self-modifying. > Ok. > > However, I have a follow-up question. The following routine *does* work: > > lcWorks: func [ /local b ] > [ > b: 0 > print "The following line s/b 0" > print b > > b: b + 1 > print "The follwing line s/b 1" > print b > ] > > It doesn't need to use "copy" to make it work. I guess this issue just > boils down to assignment techniques.
This issue doesn't have anything in common with the "assignment techniques".
> Why do I have to use different assignment techniques? That is, in the > above example, a straightforward assignment of 0 to b works as one would > expect.
The difference lies in the fact, that the expression b: b + 1 doesn't change the second value of the LCWORK function's body. In a different wording: the second value of the LCWORK's body remains unchanged, because it is Immutable. For more info on Immutable vs. Mutable see e.g. http://www.sweb.cz/LMecir/evaluation.html
> However, the same concept applied to a block, b: [ 0 0 0 ] as in > the earlier example, does not work. Instead I have to make a copy of [ 0
0
> 0 ] - b: copy [ 0 0 0 ]. > > I am not clear as to why I would need to make a copy - why wouldn't the b: > [ 0 0 0 ] just blow away the current contents, even if the function and
its
> properties are maintained between calls?
It would, but the problem lies in the fact, that the code looks different when modified. If you make a copy, any subsequent change will modify only a copy of the second value contained in the function body.
> Similarly, in one of the previous > answers from Gabriele, he showed what appears to me to be behavior of self > modifying code. However, I still don't understand why using "copy" forces > the code to update, whereas the normal assignment operator does not. >
As you might have seen above, quite the opposite is true. If you don't make a copy, a modification "updates" your code. If you make a copy, no modification of the original occurs.
> Lastly, since the code appears to be self-modifying, and behavior of > assignments change depdending upon whether it is the first call or a > subsequent one, I am curious as to what advantages this has vs the > confusion that it will cause people coming from other programming > environments. > > Thanks again, > Victor Mascari > > -- > To unsubscribe from this list, please send an email to > [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes. >
HTH Ladislav