Clojure: How to find out the arity of function at runtime?

前端 未结 7 1413
野趣味
野趣味 2021-01-31 02:08

Given a function object or name, how can I determine its arity? Something like (arity func-name) .

I hope there is a way, since arity is pretty central in C

7条回答
  •  被撕碎了的回忆
    2021-01-31 02:43

    user=> (defn test-func
             ([p1] "Arity was 1.")
             ([p1 p2] "Arity was 2.")
             ([p1 p2 & more-args] (str "Arity was " (+ 2 (count more-args)))))
    #'user/test-func
    user=> (test-func 1)
    "Arity was 1."
    user=> (test-func 1 2)
    "Arity was 2."
    user=> (test-func 1 2 3)
    "Arity was 3"
    user=> (test-func 1 2 3 4)
    "Arity was 4"
    user=> (test-func 1 2 3 4 5) ;...
    "Arity was 5"
    

提交回复
热议问题