這顯示瞭如何有一個對象或上下文中的靜態變量: 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