在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
(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
我哪里错了?作为foo函数的单参数版本的结果,我希望看到1,因为“hello!”显然是java.lang.Object的一个实例。
我认为你的协议中的第二个foo是第一个foo。重载超自然与使用略有不同的语法。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