[REBOL] Re: accessing your own function signature / context
From: joel:neely:fedex at: 24-Oct-2001 6:46
Hi, Maarten,
Maarten Koopmans wrote:
> f does not now its name its f in my case.
>
> --Maarten
> > ->> f: func [a] [print a print mold second :f]
> > ->> f "aaa"
> > aaa
> > [print a print mold second :f]
> >
> > -pekr-
> >
> > Maarten Koopmans wrote:
> > > Hi,
> > >
> > > Is there any way to access the context of a function in the
> > > function and thus access its data.
> > >
Is this a function you will write yourself? Two tricks I've
experimented with (briefly!) would require additional voodoo
and are thus beyond the scope of a simple SELF idea, but for
what they may be worth to you...
1) Pass the function as an argument to itself:
>> f: func [a b c me] [
[ c
[ print ["My sum is" a + b]
[ print ["My other arg was" mold get first second :me]
[ ]
>> f 1 2 "Hi!" :f
My sum is 3
My other arg was "Hi!"
2) Create the function in a context that retains a reference to
the function itself:
>> f: use [myself] [myself: func [a b c] [
[ c
[ print ["My sum is" a + b]
[ print ["My other arg was" mold get first second :myself]
[ ]
[ ]
>> f 2 3 "Bye!"
My sum is 5
My other arg was "Bye!"
The second option above requires copying if you want to re-use the
body block, of course...
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;