[REBOL] Re: [context] Problem with context?
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
> (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.
> calc: func [
> input [block!]
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
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]
> When executing dc/calc [5 + 2] it says:
> ** 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?
> Kind regards,
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem.