ahala
ahala

Reputation: 4793

Clojure `defn` parameter in parentheses or not

I saw a Clojure function is defined as

(defn toInt([i] (Integer. i)))

why the parameter [i] is included in parentheses? is this the same as below? any differences?

 (defn toInt [i] (Integer. i)) 

Upvotes: 5

Views: 110

Answers (1)

Michiel Borkent
Michiel Borkent

Reputation: 34800

The first uses the notation for arity overloading, but contains only one arity.

Example with two arities:

(defn my-add 
  ([x] (+ x 1))
  ([x y] (+ x y)))

(my-add 1) ;;=> 2
(my-add 1 2) ;;=> 3

Also see http://clojure.org/functional_programming (search for arity overloading).

Upvotes: 11

Related Questions