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

[REBOL] Re: in a function => was {Re: Re: to-char}

From: carl:cybercraft at: 14-Feb-2002 20:14

On 14-Feb-02, Jason Cunliffe wrote:
> From: "Carl Read" <[carl--cybercraft--co--nz]> >> > What is going on when I call it within a function and then call >> > the function repeatedly [when the previous values remain]. ie: >> > why do I _really_ need clear or copy? >> The reason can be seen in the above, in that it remains the same >> block. Meaning you haven't made a copy of the block. Continuing on >> with the above, this shows the difference 'copy makes... > Carl..thanks > ok. This is what I still don't deeply get: >>> someblock: [] > == [] >>> append someblock "a" > == ["a"] >>> append someblock "b" > == ["a" "b"] >>> append someblock "c" > == ["a" "b" "c"] >>> someblock: [] > == [] >>> someblock > == [] >>> head someblock > == [] > BUT.. when I do the same thing within a script function: > change-block: func [str [string!] /local someblock][ > someblock: [] > append someblock str > return someblock > ] >>> change-block "hello" > == ["hello"] >>> change-block "goodbye" > == ["hello" "goodbye"] > Q: Why, at the console does someblock: [] create a fresh block, but > within a script function someblock: [] persists and needs to be > remedied by copy [], clear [] or make block! 123 statements? >> More on functions: A series is created when the function is first >> created and not each time the function is called, which is why >> what's in a series will persist from function-call to function-call >> unless you specifically clear it or make a copy of it. Whether to >> use 'copy or 'clear (or neither for that matter) will depend on the >> behaviour you want from the series. > aha!, but this sounds like the second time I call my > change-block function, it is sees 'someblock: []' rebol says "oh I > already got that", yet when it reads copy, clear make it changes its > mind and redfines someblock?
I don't have a deep understanding of how functions work, but I'd guess that when functions are created space is set aside for blocks and strings and the like so that when the function is called a line like this... a-block: [] is really something like... a-block: reference-to-a-block which would just give 'a-block the block's reference, whereas... a-block: clear reference-to-a-block would clear the block of its contents before giving 'a-block its reference and... a-block copy reference-to-a-block would make a copy of the block and then give 'a-block the reference to the new block. (The old block would be discarded unless other words were also referencing it.)
> This seems pretty inconsistent to me. I know how to use it, but want > to grok further rebolzen. Please keep trying to explain me this > rudiment..
Well, I hope the above helps, and don't worry, you're certainly not the first to wonder about this. I think it's good, as it allows for more flexible functions, and you can still have them behave in a hard-wired fashion if you wish, albeit with the need for a little more thought about what you're doing. What's more, you do start to think of it as being consistant after a while. Honest! (: -- Carl Read