Hi,

you can use this:

Rebol [
    Title: "SFun"
    Author: "Ladislav Mecir"
    Email: [EMAIL PROTECTED]
    Date: 31/1/2000
    File: %sfun.r
    Purpose: {
        A function with static variables
    }
    Version: 1.0.0
    Category: [General]
]

sfun: func [
    {create a function with static local variables}
    init [block!]
    args [block!]
    body [block!]
    /local static
] [
    static: make object! init
    body: bind/copy body in static 'self
    func args head insert body static
]

{
    Example #1:

    counter: sfun [count: 0] [] [
        count: count + 1
        print count
    ]

    counter
    counter
    recycle
    counter


    Example #2

    cell: func [
        {create a function that holds a value}
        initval [any-type!]
    ] [
        sfun [value: get/any 'initval] [
            /set newval [any-type!]
        ] [
            either set [value: get/any 'newval] [
                get/any 'value
            ]
        ]
    ]

    a: cell 5
    print a
    a/set 18
    print a
}

Ladislav


> Hi,
>
> Does anyone know whether Rebol has such a kind of variable: you
initialize this
> variable at the first time you run the script. In this script,
the variable will
> be modified to some value. If you run this script again, this
variable will
> preserve the value it has in last run.
>
> Thanks in advance.
>
> Tiana
>
>
>

Reply via email to