これは、オブジェクトまたはコンテキスト内に静的変数を持つ方法を示しています: http://www.mail-archive.com/list@rebol.com/msg04764.html
しかし、一部のニーズにはスコープが大きすぎます。オブジェクト関数内に静的変数を含めることは可能ですか?
これは、オブジェクトまたはコンテキスト内に静的変数を持つ方法を示しています: http://www.mail-archive.com/list@rebol.com/msg04764.html
しかし、一部のニーズにはスコープが大きすぎます。オブジェクト関数内に静的変数を含めることは可能ですか?
または、 を使用できますFUNCTION/WITH
。これにより、関数ジェネレーターは、「self」として使用される永続オブジェクトを定義する 3 番目のパラメーターを取得します。
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 関数も参照してください。
Rebol 3 では、関数(または FUNC)ではなくクロージャー(または CLOS) を使用します。
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