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

[REBOL] context of a function Re:

From: rebol:techscribe at: 18-Aug-2000 12:55

Hi Frank, 1. The Problem: To associate the word a with the context of the f function's body, you would have to use bind. The bind function requires a sample word that is boudn to the target as its second argument. Because your body block of the function is empty, there is no sample word, and therefore there is nothing to bind to. 2. The Solution: 1. If REBOL Technologies added the following ability to the fourth function, the problem would be solved: a) Like an object has a default word self defined, which is the object itself, there should be a default word self defined for a function, and that word self should be a sample word for the function's context. b) If fourth receives a function argument, it should return the default word self that is defined for the function's context. c) We don't need to worry about people defining a local word self for the function, since that word self can take over the rule of the default word self. The fourth function will return the user defined word self instead of the default word self, and that word is just as much sample word for the local context of the function, as the default word self is. d) A problem arises if the function is intended to manipulate a global word self, since the default local word self will hide the global version of self. I haven't seen anyone complain about not being able to use a global word self from within the context of an object, and therefore I doubt that anyone will complain, if the same is true for a function. e) Another solution would be to provide a default refinement /self for every function that returns a sample word for the function's context. 3. An immediate solution We can use this approach to implement our own solution. We define a cfunc function, which emulates the func function and adds a /local word self like this: cfunc: func [spec body /local found] [ either found? found: find/tail spec /local [ insert found self ][ insert tail spec [/local self] ] insert body [self] throw-on-error [make function! spec body] ] Now we can create f as a cfunc:
>> f: cfunc [a] [] >> insert tail second :f compose [print (bind 'a first second :f)]
== []
>> f 3
3 Hope this helps, At 06:31 PM 8/18/00 +0200, you wrote:
>Hi! > >Is there a way to get the words of the context of a function? > >Example: > f: func [a] [] > g: func [a] [print a] > >Does anyone know a way to change function f AFTER its definition in that >way, that it will work like function g? > >The following does not work: > insert second :f reduce ['print first first :f] > >Because the first (and third) of a function is not bound to the functions >context. > >I think there is no direct way to get the word with binding, I could only >get the words out of functions body :( > >I am working at a serialize script, which also serializes contexts etc. > >CU, >Frank >
;- Elan [ : - ) ] author of REBOL: THE OFFICIAL GUIDE REBOL Press: The Official Source for REBOL Books http://www.REBOLpress.com visit me at http://www.TechScribe.com