A/text: B/text , then modify one changes both !! why is that happening here ....

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

  1. HCCHEN

    HCCHEN New Member

    Happens on REBOL/View Version 2.7.8.3.1 (Win32 x86)

    This example is from online ebook 'Learn REBOL' , (I add the "break the link" button to make sure a redefine can break the link.)

    view layout [
    f1: field
    btn "Display Variable" [

    ; Set the variable "t" to the text contained
    ; in the f1 field above:

    t: f1/text

    ; Now CHANGE the text in the f2 below to
    ; to equal the text stored in variable "t":

    f2/text: t
    show f1 show f2
    ]
    btn "Break the link" [
    f1/text: "I am f1"
    f2/text: "I am f2"
    show f1 show f2
    ]
    f2: field
    ]

    Follow these steps to see the problem,

    1. copy past above exercise to Rebol/view console to run it.
    2. A GUI appears. Type anything to f1 and f2 field respectively. They are independent.
    3. Click button "Display Variable" , then try again type something to both fields ... change f1 field, f2 also changed :confused: , and vise versa !!
    That's strange right?
    I don't understand this. :confused:

    4. Click "Break the link" button
    5. Now the linkage is broken. f1 and f2 are independent again.
  2. notchent

    notchent Member

    Change the line t: f1/text to t: copy f1/text

    This is a core concept in REBOL. In the original example, when t is set to f1/text, those two items represent one and the same object. When f1/text changes, t changes too - they are the exact same thing. By using the word 'copy, t is set to represent a current copy of the text in t1/text, and the words represent two separate pieces of data - changing one does not change the other.
  3. HCCHEN

    HCCHEN New Member

    'copy' fixed the problem, thank you very much !!

    Shooooo, I thought that was a bug !
    But isn't that 'core concept' even more scaring ? So I tried below tests,

    ; Now let's try assign "aa" to f1/text
    >> f1/text: "aa"
    == "aa"

    ; and then assign f1/text to f2/text ...
    >> f2/text: f1/text
    == "aa"

    ; let's see if change f1/text changes also f2/text ?
    >> f1/text: "cc"
    == "cc"

    ; f2/text is still "aa", isn't it the same thing as f1/text now?
    >> f2/text
    == "aa"

    >> f1/text: "dd"
    == "dd"
    >> f2/text
    == "aa"
    >> f2/text: "ee"
    == "ee"
    >> f1/text
    == "dd"
    >> f2/text
    == "ee"
    >>

    According to above test results, f1 and f2 are independent. This is normal to me.
    But than ... I am still in puzzle with the original question.
  4. notchent

    notchent Member

    The difference is that in the first example you are creating a new word 't. In the example above, you are setting the text property of the f2 face equal to the value of the text property of the f1 face (f2/text: f1/text). In that case, you are not defining a word to permanently point to f1/text - instead, you are setting a refinement, which points to its own value.
  5. HCCHEN

    HCCHEN New Member

    Both "f2/text: f1/text" and "[ t: f1/text f2/text: t ]" are very confusing to me. They are same weird thing actually.
    Why say so? To understand the nature of a 'field' in layouts I try one more test as shown below,
    ; --------------------------------------------------
    test4: layout [
    f1: field "F1"
    f2: field "F2"
    btn "f2/text: t: f1/text" [
    t: f1/text
    f2/text: t
    show f1 show f2
    ]
    btn "Watch" [
    print f1/text
    print f2/text
    print t
    ]
    btn "Reset f1/text" [
    f1/text: "9999"
    ]
    ]
    view test4

    ; --------------------------------------------------

    1. type '1111' to f1, '2222' to f2, they work normally.
    2. click button "f2/text: t: f1/text" then f1/text, t, and f2/text are now totally same thing. Modify f1/text or f2/text via the GUI changes all three. This is a Rebol nature. I am ok with this.
    3. click button 'Watch' print all three variables on console. No problem.
    4. Now here comes the strange thing. But it looks fine at first time and become weired after that. Click button 'Reset f1/text' , f1/text gets a value "9999", now f1/text is separated from the three. t and f2/text are still same thing.
    5. Do above step 2. again. All three variables become one thing again.
    6. Now, do above step 4. again. It doesn't work now !!!! f1/text is supposed to be "9999" but it still linked together with t and f2/text. Can't break the linkage anymore. <========= Weired !!!
  6. HCCHEN

    HCCHEN New Member

    One step closer .... Now I've found, regarding above test4, that change "f1/text: "9999"" to "set-face f1 "9999"" then it works as anticipated.
    Why value assignment statement having so many different ways? ':' , 'copy' , 'set-face' , ... maybe more ??
  7. Graham

    Graham Developer Staff Member

    You have to learn the difference in how Rebol sets words, and that as default Rebol does not copy when doing assignment.

Share This Page