Getting argument type hints of a function in Cloju

2019-06-25 16:09发布

问题:

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})