In Clojure 1.2:
(defprotocol PP
(foo [bar])
(foo [bar baz]))
=> PP
(extend-protocol PP
Object
(foo [bar] 1)
(foo [bar baz] 2))
=> nil
(foo "hello!")
=> #<CompilerException java.lang.IllegalArgumentException: No single method: foo of interface: PP found for function: foo of protocol: PP
Where am I going wrong? I'd expect to see 1 as the result from the single-argument version of the foo function, since "hello!" is clearly an instance of java.lang.Object.
I think the second foo
in your protocol is clobbering the first one. Overloading on arity has slightly different syntax than you're using.
user> (defprotocol PP
(foo [bar] [bar baz]))
PP
user> (extend-protocol PP
Object
(foo
([bar] 1)
([bar baz] 2)))
nil
user> (foo "foo")
1
user> (foo "foo" "bar")
2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With