local and global variable ..... strange in console

Discussion in 'Rebol' started by HCCHEN, Mar 22, 2011.

  1. HCCHEN

    HCCHEN New Member

    1. get to Rebol/view console,
    2. copy - past below program,
    box1: layout [
    f1: field
    ]
    box2: layout [
    f1: field
    ]
    3. Now let's see whether f1 is local or global variable.
    >> view box2 <====== a GUI appears, type '22222' into the field. Click X to terminate the GUI.

    >> f1/text <===== back to console, check the value.
    == "222222" <========== yes, f1/text as anticipated.

    >> view box1 <====== a GUI appears, type '11111' into the field, click X to close the GUI, back to console.
    >> f1/text <========== check again ...
    == "222222" <====== now strange, f1/text is still '22222'

    so, I guess we have two f1 in box1 and box2 separately, they are local objects.

    I guess, Rebol console can access f1 is a feature for debug.
    But if there's not only one f1 then Rebol/view console will be confused. I guess, it refer f1 to the last instance which in this case is box2.
  2. MaxV

    MaxV Member

    After your code and example, try this:
    Code:
    dump-face box1
    dump-face box2
    dump-face f1
    
    Inside box2 there is always a different field.
  3. MaxV

    MaxV Member

    I suppose that Rebol tend to avoid crashes, so if the same variable is in more the a layout, it keeps them separate. In case of "collisions", any action will influence just the variable in the last layout with collision name. If there aren't collisions, when you change a GUI element, you change it in any layout is. However just the first variable will be available from console.
    For large scripts that have a lot of position and face variables, it may become difficult to manage all of the names and keep them from interfering with each other. A simple solution to this problem is to define pages within objects that have the required variables defined locally to the objects. For instance here is an address book form that keeps all of its variables local:
    Code:
    make object! [
        title-name: name: email: phone: none
        num: 1
        page1: layout [
            title-name: title "Person 1:"
            box 200x3 red
            across
            text "Name"  tab name: field return
            text "Email" tab email: field return
            text "Phone" tab phone: field return
            button "Send" [
                send luke@rebol.com [
                    "Person " num newline
                    name email phone newline
                ]
                num: num + 1
                title-name/text: reform [Person num]
                clear name/text
                clear email/text
                clear phone/text
                show [title-name name email phone]
            ]
        ]
    ]
    
    from http://www.rebol.com/docs/view-guide.html#section-45

Share This Page