dev=> (macroexpand '(defn add [a b] (+ a b)))
(def add (clojure.core/fn ([a b] (+ a b))))
dev=> (defn add [a b] (+ a b))
#'dev/add
dev=> add
#object[dev$add 0x72f91c91 "dev$add@72f91c91"]
dev=> (defn add [a b]
#_=> (let [frame (first (:trace (Throwable->map (ex-info "" {}))))]
#_=> (println (demunge (name (first frame))))
#_=> (+ a b)))
#'dev/add
dev=> (add 1 2)
dev/add
3
dev=>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/5c533ccf-ec0b-4c84-b759-851f1fe3b3can%40googlegroups.com.
De: "Sean Corfield" <se...@corfield.org>
À: "clojure" <clo...@googlegroups.com>
Envoyé: Jeudi 29 Avril 2021 01:26:34
Objet: Re: How get function name in body?
Consider that:(defn add [a b] (+ a b))is expanded to (something like):(def add (fn [a b] (+ a b)))So the actual code that runs is an anonymous function, which is bound to the (global) var #'add -- the function itself has no name.
dev=> (macroexpand '(defnadd [a b] (+ a b)))
(def add (clojure.core/fn ([a b] (+ a b))))
dev=> (defnadd [a b] (+ a b))
#'dev/add
dev=> add
#object[dev$add 0x72f91c91 "dev$add@72f91c91"]
So the value of add is an object, with type dev$add, a class that implements a bunch of things including clojure.lang.IFn and java.lang.Runnable and java.util.concurrent.Callable etc.The "name" of function is hard to get to, inside a function because of that.One way to figure that out at runtime is to dig into the stack, which you can get at by creating an exception and inspecting it: be aware that this is an expensive operation!
dev=> (defnadd [a b]
#_=> (let [frame (first (:trace (Throwable->map (ex-info"" {}))))]
#_=> (println (demunge (name (first frame))))
#_=> (+ a b)))
#'dev/add
dev=> (add 1 2)
dev/add
3
dev=>
demunge is what turns Clojure's generated classnames back into readable source names.
The more important question is: why do you want the function's name inside the body?--Sean A Corfield -- (904) 302-SEAN
An Architect's View -- https://corfield.org/
World Singles Networks, LLC. -- https://worldsinglesnetworks.com/
"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
dev=> (defn add [a b]
#_=> (println (demunge (.getName (.getCallerClass (java.lang.StackWalker/getInstance java.lang.StackWalker$Option/RETAIN_CLASS_REFERENCE)))))
#_=> (+ a b))
#'dev/add
dev=> (add 1 2)
dev/add
3
dev=>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/843762456.638578.1619723272421.JavaMail.zimbra%40u-pem.fr.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/a3a4a5a8-18e8-4a07-b457-6ed58a75a800n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/CAD4thx_tnaZa5YTdojUojqEQ2ZH4_L478O9iN24MPwhxiMwVLg%40mail.gmail.com.