How to get name of argument in Clojure?

杀马特。学长 韩版系。学妹 提交于 2019-12-23 20:18:08

问题


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


回答1:


It's not possible to get the name of the var used in an outside scope within a function - the function only receives a the value passed as a parameter at runtime, not the var itself.

The only thing you could potentially do is use macros instead of functions at each level. This allows you to pass the var itself through the different macros at compile time:

(defmacro f1 [x1] `(println "hello, you passed var name >>" ~(str x1) "<<"))
(defmacro f2 [x2] `(f1 ~x2))
(defmacro f3 [x3] (let [zzz x3] `(f2 ~zzz)))

(f3 my-var)
=> hello, you passed var name >> my-var <<

This is pretty ugly - you certainly don't want to be writing all of your code with macros just to get this feature! It might make sense though in some specialised circumstances, e.g. if you are creating some kind of macro-based DSL.




回答2:


You can pass the actual var to the function rather than the var resolved value using #' reader macro as shown below:

user=> (defn f1 [x1] (println "hello, you passed var name >>" (:name (meta x1)) "<<"))
#'user/f1
user=> (defn f2 [x2] (f1 x2))
#'user/f2
user=> (defn f3 [x3] (let [zzz x3] (f2 zzz)))  
#'user/f3
user=> (def my-var 3.1414926)
#'user/my-var
user=> (f3 #'my-var)
hello, you passed var name >> my-var <<

In case you want the value bound to the var, you can use var-get function to do so.




回答3:


Why not just pass (get-var-name _symbol-here_) to functions where you will want to use the var name inside the body? For example, using exactly the same definitions of get-var-name, f2, f3, and my-var which you have given above (but changing f1 slightly):

(defmacro get-var-name [x]
  `(:name (meta (var ~x))))
(defn f1 [x1] (println "hello, you passed var name >>" x1 "<<"))
(defn f2 [x2] (f1 x2))
(defn f3 [x3] (let [zzz x3] (f2 zzz)))
(def my-var 3.1414926)
=> (f3 (get-var-name my-var))
hello, you passed var name >> my-var <<
=> nil

Maybe sometimes, you will also want to refer to the value that the symbol refers to in the function body. For example, let's say in f1 you want to also print out the value referenced by that var name. Here's how you could do that:

(defn f1 [x1] (println "hello, you passed var name >>" x1 "<<"
                       "\nwhich refers to value >>" @(resolve x1) "<<"))
=> (f3 (get-var-name my-var))
hello, you passed var name >> my-var << 
which refers to value >> 3.1414926 <<
=> nil

Note the @(resolve x1)--this is the thing returning the value that my-var refers to (and in turn my-var is the value referred to by x1).

Also, I want to mention that your current implementation of get-var-name 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?

If what I propose does not answer your question, then it seems like you don't want to have to pass (get-var-name _symbol-here_) to the functions that may end up using the var name, but rather for some reason really want to be able to do the (get-var-name ...) 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?



来源:https://stackoverflow.com/questions/11856517/how-to-get-name-of-argument-in-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!