2024 Clojure 状态调查中分享您的想法!

欢迎!请参阅关于页面以获取有关如何使用本站的一些更多信息。

0
Spec

23:11 $ clj -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.10.0-RC3"} org.clojure/test.check {:mvn/version "0.10.0-alpha2"}}}'
Clojure 1.10.0-RC3
user=> (require '[clojure.spec.alpha :as s])
nil
user=> (require '[clojure.spec.test.alpha :as st])
nil
user=> (defmulti foo identity)
#'user/foo
user=> (s/fdef foo :args (s/cat :wat string?))
user/foo
user=> (defmethod foo :bar [lol])
#object[clojure.lang.MultiFn 0x57ac5227 "clojure.lang.MultiFn@57ac5227"]
user=> (st/instrument)
[user/foo]
user=> (defmethod foo :baz [lol])
执行错误 (类转换异常) 在 user/eval150 (REPL:1).
clojure.spec.test.alpha$spec_checking_fn$fn__3026 不能转换为 clojure.lang.MultiFn
user=> *e
{:cause "clojure.spec.test.alpha$spec_checking_fn$fn__3026 cannot be cast to clojure.lang.MultiFn", :via [{:type java.lang.ClassCastException, :message "clojure.spec.test.alpha$spec_checking_fn$fn__3026 cannot be cast to clojure.lang.MultiFn", :at [user$eval150 invokeStatic "NO_SOURCE_FILE" 1]}]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
user=>


我预计这不会引发异常

2 答案

0
_评论者:urzds_

仅供参考,以便其他人也想了解适合的调用顺序(在Clojure 1.10.0和clojure.spec.alpha 0.2.176中)
{code:clj}
(require '[clojure.spec.alpha :as s])
; => nil
(require '[clojure.spec.test.alpha :as stest])
; => nil
(defmulti testfn :type)
; => #'user/testfn
(defmethod testfn :atype [m] 1)
; => #object[clojure.lang.MultiFn 0x5d16b612 "clojure.lang.MultiFn@5d16b612"]
(s/fdef testfn :args (s/cat :m (s/keys :req-un [::type ::does-not-exist])))
; => user/testfn
(stest/instrument `testfn)
; => [user/testfn]
(testfn {:type :atype :does-not-exist nil})
; => 1
(testfn {:type :atype})
; => clojure.lang.ExceptionInfo: 叫用'#$user/testfn'不符合规范。
0
...