1

这显示了如何在对象或上下文中拥有静态变量:http: //www.mail-archive.com/list@rebol.com/msg04764.html

但是对于某些需求来说范围太大了,对象函数中是否可以有一个静态变量?

4

2 回答 2

3

或者你可以使用FUNCTION/WITH. 这使得函数生成器采用第三个参数,该参数定义了一个用作“self”的持久对象:

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

您可能还想看看我的 FUNCS 函数

于 2010-07-17T12:27:53.003 回答
2

在 Rebol 3 中,使用闭包(或 CLOS)而不是函数(或 FUNC)。

在 Rebol 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
于 2010-05-18T20:54:30.443 回答