I don't know how to get the fn name at runtime, except by looking at the stacktrace which is going to expensive as you mentioned.
;; original: ;; http://groups.google.com/group/clojure/browse_thread/thread/234ac3ff0a4b6b80?pli=1 ;; but slightly changed for Clojure updates since 1.0 (defn unmangle "Given the name of a class that implements a Clojure function, returns the function's name in Clojure. Note: If the true Clojure function name contains any underscores (a rare occurrence), the unmangled name will contain hyphens at those locations instead." [class-name] (.replace (clojure.string/replace class-name #"^(.+)\$([^@]+)(|@.+)$" "$1/$2") \_ \-)) ;; only appropriate for debugging (defmacro current-fn-name [] "Returns a string, the name of the current Clojure function" `(-> (Throwable.) .getStackTrace first .getClassName unmangle)) If you have control over the source code, defining your own version of defn will work better. I choose %0 as my local symbol for the fn name, inspired by the convention for args to anonymous functions. I found this useful for some pre-conditions and error-reporting. ;; %0 is bound to the function's name within the function. (defmacro defn0 [name & fdcls] `(let [~'%0 (symbol (name (ns-name *ns*)) (name '~name))] (defn ~name ~@fdcls))) Steve Miner stevemi...@gmail.com On Mar 28, 2012, at 11:02 AM, Shantanu Kumar wrote: > Hi, > > Is it possible to write a macro that when used in a top-level function > (created using defn) can find out the name of the immediate top-level > function? I know *ns* returns the namespace and it's possible to walk > the current thread's stack trace to find out the function name, but I > am looking for a computationally cheap way to do it. > > Thanks, > Shantanu > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en