Hi, Rebols,

while writing something more complicated, I came to this. You may
find some use for it:

Rebol [
    Title: "SFun"
    Author: "Ladislav Mecir"
    Email: [EMAIL PROTECTED]
    Date: 31/1/2000
    Purpose: {
        A "replacement" for a GC-proof Use
    }
    Version: 1.0.0
]

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
        ]
    ]
]

{
    Cell test:

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

; Ladislav

Reply via email to