[REBOL] Re: Static Variable Inside a Function
From: joel:neely:fedex at: 2-Dec-2002 13:54
Hi, Philippe,
Philippe Oehler wrote:
> Is there a way to have a Static Variable' C-like inside a
> function to count the number of time a function is called ?
>
Romano beat me to the suggestion of using an object instead of a
simple function, but just for hack value, here's a function-only
variation. (I recommend the object approach, needless to say! ;-)
foo: func [arg [string!] /reset /report /local ncalls][
ncalls: [0]
if reset [return ncalls/1: 0]
if report [return ncalls/1]
ncalls/1: ncalls/1 + 1
rejoin ["call # " ncalls/1 " with " arg]
]
which behaves as
>> foo "Hello"
== "call # 1 with Hello"
>> foo "world"
== "call # 2 with world"
>> foo "goodbye"
== "call # 3 with goodbye"
>> foo/report ""
== 3
>> foo/reset ""
== [0]
>> foo "It's me again!"
== "call # 1 with It's me again!"
-jn-
--
----------------------------------------------------------------------
Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446