What exactly happened when not using "copy" to deal with a string?
Asked Answered
S

2

9

The purpose of the function below is to return a string having inserted the argument value between two stars.

star-name: func [name /local stars] [
    stars: "**"
    insert next stars name
    stars
]
print star-name "test" ;*test*
print star-name "this" ;*thistest*, but what I really want is *this*  

The second time I call the function, the argument of the first call still remains inserted. I know the answer is to use copy "**". My question is, doesn't it reassign the stars variable to "**" every time the function is called?

Scar answered 9/8, 2013 at 7:7 Comment(0)
F
6

In the case of the function there is just one "**" string definition. That definition is used by Rebol load function just once, since load is run just once to translate the code to Rebol internal form - a block. It is true that the assignment occurs twice if you call the function twice, but the assignment does not create anything, it just makes the variable refer to the same string again.

In your comment you should notice that you actually have two "**" string definitions leading to two strings being created by load. If you use

code: [stars: "**" insert next stars something]
something: "this"
do code
something: "that"
do code

you will notice that there is just one string definition and while you do not have any function the behaviour is the same as it was when a function was used.

Foreshorten answered 9/8, 2013 at 9:19 Comment(2)
I think I have got the idea. In Rebol, functions are just blocks. It's quite different from the functions in other languages. Thanks a lot :)Scar
Some insight into why Rebol behaves this way: Is Rebol a Pure Functional LanguageRemarkable
C
2

If you use a set-word on a series, then the default behavior is to allocate the memory for that series just the once. This allows you to use that as a static variable that persists between function calls as you have found.

If you don't want that behavior, then you need to explicitly copy the series to create a new series each time.

This is another way you can do this as the stars local is not required

star-name: func [ name ][
  rejoin [ "*" name "*" ]
]
Coranto answered 9/8, 2013 at 7:50 Comment(2)
Then it's the feature of function? Because I just write: stars: "**" insert next stars "this" print stars stars: "**" insert next stars "that" print stars, would have no the above problem.Scar
In this you now have two different series at two different memory addresses.Coranto

© 2022 - 2024 — McMap. All rights reserved.