Home   Archive   Permalink



Can a function find its own name?

I am looking for things I can put it a program so that if the program crashes to a command prompt I can probe around for helpful information. It occurred to me that if, when I call a function, that function could make a note of its own name in some place, then I could probe that 'some place' to find the last function called. Of course I could just hard-code the function name, but where would the fun be in that:
    
FUNCTION-1: does [
     MOST-RECENT-FUNCTION-CALL: copy 'FUNCTION-1'
     ...other function code...
]
    
Thank you.

posted by:   Steven White       10-Feb-2016/11:53:13-8:00



It canít because the function is anonymous, it doesnít have any name. What you have is word FUNCTION-1 that holds value of type FUNCTION!.
    
To explain it bit more: you can do FUNCTION-2: :FUNCTION-1 and now both FUNCTION-1 and FUNCTION-2 point to same function. What should it log in this case? So if you want to log it somehow, some hardcoded solution is probably the easiest way to go.

posted by:   rebolek       10-Feb-2016/12:24:47-8:00



As rebolek says, functions do not necessarily have names.
    
When they do, this R2 trick gets you the name:
    
function-1: func [][print get in disarm try [0 / 0] 'where]
    
The trick works because the error! returned from the try [0 / 0] contains the name of the function - if it has one.

posted by:   Sunanda       10-Feb-2016/13:24:24-8:00



Thank you both. This hits my "third-generation" thinking that I have so much trouble getting out of.    
    
If I pursue this idea I will go with the hard-coding because it is more obvious to the reader.

posted by:   Steven White       10-Feb-2016/13:47:05-8:00



R3-Alpha had a STACK function which can give you the name of the word a function was invoked with...if you happened to invoke the function with a word:
    
     >> foo: func [] [stack 0]        
     >> bar: func [] [foo]
     >> baz: func [] [apply :bar []]
     >> baz
     == [stack foo -apply- apply baz]
    
Ren-C has much more powerful features in the form of BACKTRACE and FRAME!.
    
     >> foo: func [x y] [
         frame: context-of 'x
         print ["The type of frame is" type-of frame]
         print mold frame
         print ["The frame was labeled with" label-of frame]
     ]
    
     >> foo 10 "Twenty"
    
     The type of frame is frame!
     make frame! [
         x: 10
         y: "Twenty"
         return: ...
     ]
     The frame was labeled with foo
    
Debug features are new developments, being worked on alongside other foundational fixes, and will be a priority after "specific binding", which will hopefully be solved once and for all...eliminating the need for CLOSURE!.
    
https://github.com/metaeducation/ren-c/wiki/Relative-Binding-and-FRAME!-Internals

posted by:   Fork       10-Feb-2016/15:07:35-8:00



your site|your web blog|your web page|your web site|your website} then before long came up with a terrible feeling I never expressed respect to the site owner for those secrets. Those guys happened to be absolutely very interested to read them and have now undoubtedly been loving them. <a href="http://www.skornikeairmax90rea.com">nike air max 90 dam rea</a> Many thanks for getting so kind

posted by:   nike air max 90 dam rea       24-Aug-2016/20:41:52-7:00



1. >> ? trace
    
2. >> ? disarm
    
3. >> ? throw-on-error
    
Let the RVC work for you.

posted by:   Time Series Lord       4-Oct-2016/18:30:38-7:00