Skip to content

Instantly share code, notes, and snippets.

@dakrone
Created May 19, 2010 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakrone/406504 to your computer and use it in GitHub Desktop.
Save dakrone/406504 to your computer and use it in GitHub Desktop.
(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
Copy link

kotarak commented May 20, 2010

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

@dakrone
Copy link
Author

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