Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get name of argument in Clojure?
    primarykey
    data
    text
    <p>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.</p> <p>So I would like to do something like (academic example):</p> <pre><code>(defn f1 [x1] (println "hello, you passed var name &gt;&gt;" (get-var-name x1) "&lt;&lt;") (defn f2 [x2] (f1 x2)) (defn f3 [x3] (let [zzz x3] (f2 zzz)) (def my-var 3.1414926) (f3 my-var) user&gt; hello, you passed var name &gt;&gt;my-var&lt;&lt; </code></pre> <p>I'm able to do this macro based on some stuff i found:</p> <pre><code>(defmacro get-var-name [x] `(:name (meta (var ~x)))) </code></pre> <p>This works when called eg from the REPL, but compiler chokes when called from an "inside" scope eg</p> <pre><code>(defn another-func [y] (get-var-name y)) </code></pre> <p>Compiler says "saying Unable to resolve var y". <code>(macroexpand...)</code> shows it's trying to find local variable y in the current namespace, rather than the original variable in the current namespace. I think <code>(var...</code>) looks for namespace vars only, so this prevents the macro from working either within a function or another binding such as <code>let</code>.</p> <p>I <em>think</em> 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.</p> <p>thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload