Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: [context] Problem with context?

From: apwing::zonnet::nl at: 4-Apr-2005 14:08

Volker Nitsch wrote:
>On Apr 4, 2005 12:04 PM, Arie van Wingerden <[apwing--zonnet--nl]> wrote: > >>Hi rebolers, >> >>being busy with some dialecting I bumped into a kind of context >>(variable scope) problem. >>Here follows a small example: >> >>========================================================================================================================================= >>REBOL [] >> >>dc: make object! [ >> rule: [set num1 number! '+ set num2 number! (print num1 + num2 res: >>insert tail res (num1 + num2))] >> >> > >Remember that everything is by default bound to the global context. >you do not need to declare it. so num1, num2 and res are global, but >have no value. but the first thing you do, is set values to num1 and >num2. but res stays valueless, the first thing you do is reading it. >-> error. > >> calc: func [ >> input [block!] >> /local >> res >> num1 >> num2 >> ][ >> >> > >The locals are not known to the rule. you can do two things here: > bind rule 'res ; or some other local word >then all locals are visible in the rule. >or better, put them in the object: > dc: make object! [ > num1: num2: res: none > rule: [..] > calc: func [input [block!]][..] > ] >rule is defined inside the object, so its bound to it. > >> res: copy [] >> if not parse input rule [ >> res: "Bad input!" >> ] >> print head res >> ] >>] >> >>dc/calc [5 + 2] >> >>halt >>========================================================================================================================================= >> >>When executing dc/calc [5 + 2] it says: >> 7 >> ** Script Error: res has no value >> ** Where: calc >> ** Near: res: insert tail res (num1 + num2) >> >>Which means that variables num1 and num2 are having a value, added and >>printed (7), >>but res has no value. >> >>However res is defined in dc/calc and is being initialized properly >>right before executing the parse. >> >>When I define res at the same level as I did rule and not within clc, >>everything works fine. >> >>Any hints to improve my understanding? >> >>TIA >> >>Kind regards, >> Arie >>-- >>To unsubscribe from the list, just send an email to >>lists at rebol.com with unsubscribe as the subject. >> >> >> >> >
Hi Volker, obviously I had a wrong view on these things. To better understand it I now moved RULE from outside to inside the function and then it works properly, because the words are bound to the function context. Your suggestion to put all these variables right in the object (outside the function) is also OK. Thanks for helping me understand that great miracle called REBOL a bit more again! Kind regards, Arie