[REBOL] Re: Confused over Contexts
From: nitsch-lists:netcologne at: 22-Dec-2001 15:37
RE: [REBOL] Confused over Contexts
[philb--upnaway--com] wrote:
> Hi Guys,
>
> I have a simple progam ...
>
> rebol []
> fn-win: func [/local lv-fld]
> [
> view/new layout
> [
> lv-fld: field
> button "Value" [print lv-fld/text]
> ]
> ]
>
fn-win: func []
[
context[
lv-fld: none
view/new layout
[
lv-fld: field
button "Value" [print lv-fld/text]
]
]
]
difference is, functions share the context for their locals,
only saving/restoring content for recursion.
so if a function gives a word to the outside,
all blocks share the same word.
with context a real new word is created each time.
also RT says they may cange function-behavior,
setting all locals to none on return.
so using locals in code which "lives" after return may break then.
> main: view layout
> [
> button "Window" [fn-win]
> ]
>
> 1. Run program to open Window #1
> 2. Press button to open a Window #2
> 3. On Window #2, Enter "AAAA"
> 4. Press Button on window #2
> 5. Program prints "AAAA"
> 6. Press button on Window #1 to Open Window #3
> 7. On Window #3, Enter "BBBB"
> 8. Press Button on Window #3
> 9. Program prints "BBBB"
> 10. Press Button on Window #2 (again)
> 11. Prigram prints "BBBB"
> :-(
>
> Even though I declare lv-fld as local the there only seems to be 1 version of lv-face
??
>
> Is there a simple solution to this problem ?
>
> Cheers Phil
>
-Volker