(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 |
This comment has been minimized.
This comment has been minimized.
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
This comment has been minimized.
Try
(extend R P {:foo (fn ([this a] 1) ([this a b] 2))})
.