Created
May 19, 2010 16:35
-
-
Save dakrone/406504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, thanks Kotarak, I updated the gist with that example.