hey, thats tips! funally i can dump my functions after error!
Thanks, Elan!
.. ugly addings by me :)
[REBOL [
title: "a func with dumpable context :)"
author: "Elan, Volker"
purpose: {
[dump-func a-func] will source & dump locals, if defined
with debug-func (alias Elan's cfunc). you can set
func: :debug-func because there is a original-func
set too.
}
version: 0.0.0.4
file: %/home/volker/w/rebol/dump-func.r
date: 19-Aug-2000/13:32:44+1:00
]
if not value? 'original-func [original-func: :func]
dump-func: func ['f1] [
do compose [source (f1)]
f1: get f1
locals: copy first :f1
bind locals first second :f1
? locals
forall locals [if word? first locals [
probe first locals probe get first locals]]
]
[ ;Elan, bit modified
]
debug-func: func [spec body /local found] [
either found? found: find/tail spec /local [
insert found [function-self]
][
insert tail spec [/local function-self]
]
insert body [function-self]
make function! spec body
]
print "---"
f1: debug-func [a b] []
f1 "the a" "the b"
dump-func f1
f2: debug-func [/local a /opt b] [a: "in f2"]
f2
dump-func f2
[; or use func allways, but
func: :debug-func
func: :original-func
]
()]
--- [EMAIL PROTECTED] wrote on 18-Aug-2000/12:55:03-7:00
> 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
>
>
>
>