I would like to get the name of a var defined outside a function, from within a function. The name should be the name that I used at the scope of the original definition, not any nested bindings where I'm actually trying to use the name.
So I would like to do something like (academic example):
(defn f1 [x1] (println "hello, you passed var name >>" (get-var-name x1) "<<")
(defn f2 [x2] (f1 x2))
(defn f3 [x3] (let [zzz x3] (f2 zzz))
(def my-var 3.1414926)
(f3 my-var)
user> hello, you passed var name >>my-var<<
I'm able to do this macro based on some stuff i found:
(defmacro get-var-name [x]
`(:name (meta (var ~x))))
This works when called eg from the REPL, but compiler chokes when called from an "inside" scope eg
(defn another-func [y]
(get-var-name y))
Compiler says "saying Unable to resolve var y". (macroexpand...)
shows it's trying to find local variable y in the current namespace, rather than the original variable in the current namespace. I think (var...
) looks for namespace vars only, so this prevents the macro from working either within a function or another binding such as let
.
I think I'm stuck having to manually get the variable name from the same scope where I define the variable and pass it along as an extra parameter. Is there a more elegant way to pass var name information through a chain of bindings to the point where it's used? That would be bad-ass.
thanks