Getting argument type hints of a function in Clojure

青春壹個敷衍的年華 提交于 2020-01-14 13:06:41

问题


I'm looking to extract the type hint information of a function's arguments, but I can't seem to find a way to access that information.

For example, say I have the following function:

(defn ^Double do-something [^String a, ^String b]
  5.0)

Pulling the tag is straightforward:

(:tag (meta #'do-something)) ; => java.lang.Double

For the arguments, however, something like this won't work:

(:arglists (meta #'do-something)) ; => ([a b])

This just gives me the arguments and not the type information. Is there a way to get the type of a and b?

The reason I want to do this is because I'm writing a tool to analyze/document functions, and if a function is type hinted, I'd like to know that.

Adding type hints to code for the sole purpose of documentation seems like not a particularly great idea, but I'd just like to use the information if it's already there in the first place and if no other type information is present (such as perhaps if core.typed was used).


回答1:


You need to get the metadata of the arguments:

user=> (map meta (first (:arglists (meta #'do-something))))
({:tag String} {:tag String})


来源:https://stackoverflow.com/questions/26116277/getting-argument-type-hints-of-a-function-in-clojure

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