Given a function x in clojure how can I programatically get a count of the number of arguments?
eg:
(fn a [b c] ...) has has two arguments
(fn a [] ...) has has zero arguments
Given a function x in clojure how can I programatically get a count of the number of arguments?
eg:
(fn a [b c] ...) has has two arguments
(fn a [] ...) has has zero arguments
If you have access to the var that holds the function you can get the argument count by accessing its metadata in the following way:
arities
will return a seq with all the arities for the function. This has the disadvantage that for a variadic argument vector ([_ _ & _]
) it will return (4).This could be fixed by removing the
&
symbol from all the argument lists.If you don't have access to the var, the following is a little function that I've used to detect the argument count of a function. It uses reflection so it's not the approach you might want to take if you need good performance. Also take into account that it relies on implementation details.
EDIT
After giving the answer another read, I realized the result 3 for a variadic function defined as
(defn x [_ _ & _] ,,,)
, is actually quite misleading since it's the same result you would get for a function with 3 arguments. The following version will return:variadic
, instead of a specific number, for the argument vectors that contain the&
symbol (except for the case[&]
where&
it's the actual argument name). As mentioned in a comment by Jeremy Heiler getting the argument count from the metadata only works if the value for:arglists
is not manually changed.The reflection version for this is a little more complicated and it relies on more implementation details (i.e. "Is this or that function declared?" or "Does the function extend the class X?"), so I wouldn't recommend using that approach.
You could construct a function taking all arguments, putting them in a list and returning the count like so: