can we fix that annoying rebol's behaviour?

Discussion in 'Rebol' started by YueM, May 30, 2010.

  1. YueM

    YueM New Member

    I always find this Rebol behaviour very annoying actually it's my biggest pet peeve .i.e your local variable gets overriden when you call your function and then keeps the value for the next run as well. can't this be fixed ?


    test: func [ input /local s ] [ s: "hello" append s input ]
    >> test "joe"
    == "hellojoe"
    >> test "tom"
    == "hellojoetom"

    test "tom" should give me "hellotom"

    I know we have to do "copy" to work around that problem, but this is a hack, it goes against the principle that our program should do exactly as we write. i.e if I have a statement s: "dear" in the body of my function, every time you call the function , it should execute s: "dear" and assign "dear" to s.

    this brings an element of unpredictability to the outcome of my code, if the language can override what I want my code to do.

    what does the community think about that?

    p.s I have also raised the same issue on http://rebolforum.com to get more eyeballs, and feedback from the rebol community.
  2. Graham

    Graham Developer Staff Member

    It would break every script out there ...
  3. Graham

    Graham Developer Staff Member

    BTW, if you use a closure instead of function, then you create new variables each time and then you can use your desired behaviour.
  4. Graham

    Graham Developer Staff Member

    Code:
    
    >> test: closure [ input /local s ][ s: "hello" append s input ]
    >> test "joe"
    == "hellojoe"
    >> test "tom"
    == "hellotom"
    
  5. notchent

    notchent Member

    Hi Yuem,

    Whenever you want to create a fresh, new, empty block or string, just use "copy". It's that simple :)

Share This Page