[racket] Object's self name

From: Ryan Culpepper (ryan at cs.utah.edu)
Date: Tue Jul 5 03:09:21 EDT 2011

On 07/05/2011 12:45 AM, Sergey wrote:
> Hi all.
> Is there a way to get function's name?
>
> (define (fn) ...)
> How can I retrieve "fn" inside itself body?

You can use object-name to get the name of a function, if it has one.

 > (object-name fn)
'fn
 > (object-name (lambda (x) x))
#f

There's no way to ask "what function am I in?"; you have to handle 
getting a reference to the function yourself. That's no problem with 
defined functions, though, because the function name is in scope within 
the function's body:

 > (define (fn) (format "I am called ~s." (object-name fn)))
 > (fn)
"I am called fn."

Ryan


Posted on the users mailing list.