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

[REBOL] Re: Creating functions with local variables

From: joel:neely:fedex at: 2-Jan-2003 9:56

Hi, Robert, Happy New Year! Interleaved below are a few hints that I hope are relevant (and not too elementary/outdated to be useful to you... ;-) Robert M. Muench wrote:
> > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]] > > On Behalf Of Anton > > > 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 >
Since DICE2 appears as a set-word inside the MAKE OBJECT! that word is bound to the new context and (inside ACTION-WITH-VAR2) will be the local one, rather than the global one ...
> But if you comment one line: > > dice2: 3 > > test: context [ > ; ### dice2: 2 > action-with-var2: [dice2: 2 print ["Dice2:" dice2]] > ] > > ?? dice2 > ; >> dice2: 3 >
... hence the difference obtained when the set-word is commented out in the object definition.
> 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. >
Here is a little sample that uses multiple tricks to work around some of the issues that I inferred are in play here: 8<-------- REBOL [] glofun: none do in make object! [ counter: 0 set 'glofun locfun: func [] [counter: counter + 1] configure: func [n [integer!]] [counter: n] ] 'configure to-integer now/time 8<-------- I saved the above in a file named "glopri.r" and then called for it to be evaluated, with the following results:
>> do %glopri.r
Script: "Untitled" (none) == 35292
>> glofun
== 35293
>> glofun
== 35294
>> glofun
== 35295 Hope this helps! -jn-