下面这个函数的目的是返回一个在两个星之间插入参数值的字符串.
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*
Run Code Online (Sandbox Code Playgroud)
第二次调用函数时,第一次调用的参数仍然是插入的.我知道答案是使用copy "**".我的问题是,每次调用函数时,它是否都不会重新分配stars变量"**"?
在函数的情况下,只有一个"**"字符串定义.该定义仅由Rebol load函数使用一次,因为load只运行一次将代码转换为Rebol内部形式 - 一个块.确实,如果你调用函数两次,赋值会发生两次,但赋值不会创建任何东西,它只是让变量再次引用相同的字符串.
在你的评论中你应该注意到你实际上有两个"**"字符串定义导致两个字符串被创建load.如果你使用
code: [stars: "**" insert next stars something]
something: "this"
do code
something: "that"
do code
Run Code Online (Sandbox Code Playgroud)
你会注意到只有一个字符串定义,当你没有任何函数时,行为与使用函数时的行为相同.