[REBOL] Re: Creating functions with local variables
From: robert:muench:robertmuench at: 31-Dec-2002 11:39
> -----Original Message-----
> From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]
> On Behalf Of Anton
> Sent: Tuesday, December 31, 2002 5:56 AM
> To: [rebol-list--rebol--com]
> Subject: [REBOL] Re: Creating functions with local variables
> context [myvar: id * 5 print myvar]
>
> context should automatically pick up set-words
> and bind them to the new local context.
Hi, I played around with context too but wasn't able to get it to work
as I liked. Here is what I tried:
dice2: 3
test: context [
dice2: 2
action-with-var2: [dice2: 2 print ["Dice2:" dice2]]
]
?? dice2
; >> dice2: 3
myfunct: 'action-with-var2
do test/:myfunct
; >> Dice2: 2
?? dice2
; >> dice2: 3
But if you comment one line:
dice2: 3
test: context [
; ### dice2: 2
action-with-var2: [dice2: 2 print ["Dice2:" dice2]]
]
?? dice2
; >> dice2: 3
myfunct: 'action-with-var2
do test/:myfunct
; >> Dice2: 2
?? dice2
; >> dice2: 2
So you still have to mention all local words. And you have to agree on a
function that gets called because the context will be an object that
doesn't execute any code if used. You have to explicitly call a word
from the context.
The problem with your approach is: How do you execute it at runtime? It
only works if pasted to command line.
>> id: 3
== 3
>> test: context [myvar: id * 5 print myvar]
15
>> test
>> do test
>> reduce test
>> ?? test
test:
make object! [
myvar: 15
]
Robert