What's the use of the second argument on extend-protocol
1 2 3 4 5 6 7 8 | (defprotocol my-protocol (foo [x])) (extend-protocol my-protocol java.lang.String ; this (foo [x] (.length x))) (foo"fooooo") ; 6 |
已转换为:
1 2 3 4 5 6 7 8 9 | (defprotocol my-protocol (foo [x])) (extend-protocol my-protocol java.lang.Long ; this (foo [x] (.length x))) (foo"fooooo") ; it gave an output 6, while I expect it will throws, since I'm extending from Long, where it doesn't have length() ; In the end, it will just check on the args ? (in this case it's x) |
我给了它
如果将
1 2 3 | ;; calling foo with a Long: user=> (foo 1) IllegalArgumentException No matching field found: length for class java.lang.Long clojure.lang.Reflector.getInstanceField (Reflector.java:271) |
如果不将协议扩展到
1 2 3 | ;; calling foo with a String with no implementation provided: user=> (foo"asdf") IllegalArgumentException No implementation of method: :foo of protocol: #'user/my-protocol found for class: java.lang.String clojure.core/-cache-protocol-fn (core_deftype.clj:537) |
当然,如果先将
Clojure不会尝试静态地确定