Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(ns multiaritytest)
; This works great.
(defprotocol P
(foo [this a] [this a b]))
(deftype R []
P
(foo [this a] (println "hi"))
(foo [this a b] (println "hi2")))
(def r (R.))
(foo r :a)
(foo r :a :b)
;=> hi
;=> hi2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This does not.
(defprotocol P
(foo [this a] [this a b]))
(defrecord R [])
(extend-protocol P R
(foo [this a] (println "hi"))
(foo [this a b] (println "hi2")))
(def r (R.))
(foo r :a)
(foo r :a :b)
;=> java.lang.IllegalArgumentException: Wrong number of args passed to: multiaritytest$eval--84$fn (multiaritytest.clj:0)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This works great also. (thanks Kotarak!)
(defprotocol P
(foo [this a] [this a b]))
(defrecord R [])
(extend R P
{:foo (fn
([this a] (println "hi"))
([this a b] (println "hi2")))})
(def r (R.))
(foo r :a)
(foo r :a :b)
;=> hi
;=> hi2

kotarak commented May 20, 2010

Try (extend R P {:foo (fn ([this a] 1) ([this a b] 2))}).

Owner

dakrone commented May 20, 2010

Awesome, thanks Kotarak, I updated the gist with that example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment