[REBOL] Re: Reflectivity: How a function can know its own name ?
From: sunandadh:aol at: 27-Mar-2002 4:05
> Thanks for your answer. In your examples you show that in SOME cases, the
> name concept can be a problem. But It would seem logical for me that the
> system implements an instruction for a function to know its own name when
> is possible.
I agree. I used the term "problematic concept" when referring to the "name"
of a function, because it's a problem for Rebol (which often can't tell for
reasons already stated), developers (who know when a function has a name, and
just want to get hold of it) and support staff (who want precisely pinpointed
error information when code fails -- including, of course, the current
function name, if there is one).
Someone (sorry I can't find the original post to credit you) came up with a
method of finding the function name -- if it exists -- by forcing an error
and grabbing the 'where value from the error object.
This shows that the name of a function -- where it exists -- is available.
It's a pity that it isn't more easily accessible. You could always
encapsulate the code below into a function-name? word, and then ask RT to
make it native for speed,
Here's some sample code based on the half-remembered post. You'll see some of
the problematic issues for an unnamed function. But if your functions -- like
most of mine -- have a name, this may work for you.
myfunc1: func  [
print ["my name is "
get in disarm try [0 / 0 ] 'where
;; name of function identical to func1
;; name of function in a block -- various forms
myfuncs: copy 
loop 5 [append myfuncs :myfunc1]
foreach f myfuncs [f]
for nn 1 length? myfuncs 1 [myfuncs/:nn]
for nn 1 length? myfuncs 1 [do pick myfuncs nn]
;; name of function in an object
myobj: make object! [objfunc: ""]