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

[context] Problem with context?

 [1/5] from: apwing::zonnet::nl at: 4-Apr-2005 12:04


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))] calc: func [ input [block!] /local res num1 num2 ][ 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

 [2/5] from: volker::nitsch::gmail::com at: 4-Apr-2005 12:57


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
<<quoted lines omitted: 5>>
> 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!]
<<quoted lines omitted: 3>>
> 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 [
<<quoted lines omitted: 25>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- -Volker Any problem in computer science can be solved with another layer of indirection. But that usually will create another problem. David Wheeler

 [3/5] 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,
<<quoted lines omitted: 77>>
>> >>
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

 [4/5] from: antonr::lexicon::net at: 5-Apr-2005 0:20


Hi Arie, If you had other functions in the object which also used the rule, then it would make sense to share the rule in the way Volker suggested: parse input bind rule 'res which binds to the function's context (using the local word 'res as the known-word). If there's only ever going to be one function, then you might as well make all your rules be defined in the function body as locals, then you can probably dispense with the enclosing object altogether! The fact that you did put the function in an object to start with makes me think you did want to share some parts like the rule... Anton.

 [5/5] from: apwing::zonnet::nl at: 4-Apr-2005 17:00


Anton Rolls wrote:
>Hi Arie, >If you had other functions in the object
<<quoted lines omitted: 29>>
>> >>
Hi Anton, in fact I started putting it all in an object because I saw an example showing it. In the mean time I tried a few variants (e.g. the one you mention without a surrounding object). Your points are clear. Of course you are right ;-) Thank you! Kind regards, Arie

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted