这显示了如何有一个对象或上下文中的静态变量: http://www.mail-archive.com/[email protected]/msg04764.html在rebol函数中可以有静态变量吗?
但范围过大的一些需求,是有可能有一个目标函数内的静态变量?
这显示了如何有一个对象或上下文中的静态变量: http://www.mail-archive.com/[email protected]/msg04764.html在rebol函数中可以有静态变量吗?
但范围过大的一些需求,是有可能有一个目标函数内的静态变量?
在雷博尔3时,使用一个封闭件(或CLOS),而不是一个函数(或FUNC)。
在雷博尔2,假的具有包含您的静态值块,如:
f: func [
/local sb
][
;; define and initialise the static block
sb: [] if 0 = length? sb [append sb 0]
;; demonstate its value persists across calls
sb/1: sb/1 + 1
print sb
]
;; sample code to demonstrate function
loop 5 [f]
== 1
== 2
== 3
== 4
== 5
或者您可以使用FUNCTION/WITH
。这使得函数发生器需要第三个参数,它定义了用作“自我”一个持久化对象:
accumulate: function/with [value /reset] [
accumulator: either reset [
value
] [
accumulator + value
]
] [
accumulator: 0
]
要使用它:
>> accumulate 10
== 10
>> accumulate 20
== 30
>> accumulate/reset 0
== 0
>> accumulate 3
== 3
>> accumulate 4
== 7
你也可能想看看在my FUNCS function。
正在修复FUNCT名称更正和[添加累加器使用示例](http://stackoverflow.com/revisions/3271463/2) 。 *(根据它的政策[可以将答案添加到示例中](http://meta.stackexchange.com/questions/20447/))*没有由@Ladislav代言明示或暗示。 :-) – HostileFork 2014-02-16 02:16:58
聪明,谢谢。 (我仍然在使用R2,因为R3似乎不适用于我的电脑) – 2010-05-18 21:03:34