Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting argument type hints of a function in Clojure

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

like image 768
reibitto Avatar asked Sep 30 '14 08:09

reibitto


1 Answers

You need to get the metadata of the arguments:

user=> (map meta (first (:arglists (meta #'do-something))))
({:tag String} {:tag String})
like image 128
Tomo Avatar answered Nov 05 '22 09:11

Tomo