Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why not just pass <code>(get-var-name _symbol-here_)</code> to functions where you will want to use the var name inside the body? For example, using exactly the same definitions of <code>get-var-name</code>, <code>f2</code>, <code>f3</code>, and <code>my-var</code> which you have given above (but changing <code>f1</code> slightly):</p> <pre><code>(defmacro get-var-name [x] `(:name (meta (var ~x)))) (defn f1 [x1] (println "hello, you passed var name &gt;&gt;" x1 "&lt;&lt;")) (defn f2 [x2] (f1 x2)) (defn f3 [x3] (let [zzz x3] (f2 zzz))) (def my-var 3.1414926) =&gt; (f3 (get-var-name my-var)) hello, you passed var name &gt;&gt; my-var &lt;&lt; =&gt; nil </code></pre> <p>Maybe sometimes, you will <em>also</em> want to refer to the value that the symbol refers to in the function body. For example, let's say in <code>f1</code> you want to also print out the value referenced by that var name. Here's how you could do that:</p> <pre><code>(defn f1 [x1] (println "hello, you passed var name &gt;&gt;" x1 "&lt;&lt;" "\nwhich refers to value &gt;&gt;" @(resolve x1) "&lt;&lt;")) =&gt; (f3 (get-var-name my-var)) hello, you passed var name &gt;&gt; my-var &lt;&lt; which refers to value &gt;&gt; 3.1414926 &lt;&lt; =&gt; nil </code></pre> <p>Note the <code>@(resolve x1)</code>--this is the thing returning the value that <code>my-var</code> refers to (and in turn <code>my-var</code> is the value referred to by <code>x1</code>).</p> <p>Also, I want to mention that your current implementation of <code>get-var-name</code> will throw Exceptions when the argument passed to it is either not a symbol, or is a symbol, but is not currently bound to a value. Is this the behavior you want?</p> <p>If what I propose does not answer your question, then it seems like you don't want to <em>have</em> to pass <code>(get-var-name _symbol-here_)</code> to the functions that may end up using the var name, but rather for some reason really want to be able to do the <code>(get-var-name ...)</code> from within the function body. If this is the case, why is it that you want to be able to do it that way? Or, if you feel I have not answered your question for some other reason, what is that reason?</p>
 

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