我想將core.type註解應用到我的代碼中,但是我正在如何/何時實例化一個多態的核心函數,並從函數體內調用,進入一個絆腳石。如何在Clojure的core.typed中註釋多態核心函數?
排除故障,我已經瞭解到,我必須處理過濾器和計數特殊,因爲它們分別是多態和靜態的,匿名函數應該被拉出並註釋在綁定中。如果有人能夠根據下面的錯誤消息的輸出解釋如何對其進行註釋,我將非常感激。
這裏是我的別名:
(defalias Key-set (Set (Option Kw)))
(defalias Player (Ref1 Key-set))
(defalias Set-vec (Vec (Set Kw)))
(defalias Move (U ':x ':o))
目前的形式,該函數是在:
(ann first-two [Player -> (Option Seq)])
(defn first-two [player]
(let [;; best guesses here
filter> (inst filter [[(U (Seqable Any) clojure.lang.Counted nil) -> Bool] -> Any] (Option (clojure.lang.Seqable [(U (Seqable Any) clojure.lang.Counted nil) -> Bool])))
count> (ann-form count [(U nil (Seqable Any) clojure.lang.Counted) -> Number])
two-count? :- [(U nil (Seqable Any) clojure.lang.Counted)-> Bool], #(= (count> %) 2)
couples :- (Option (Seqable Key-set)), (concat adjacents opposite-corners opposite-sides)]
(first (filter> two-count?
(for [pair :- Key-set, couples]
(clojure.set/intersection pair @player))))))
類型檢查錯誤消息:
Type Error (tic_tac_toe/check.clj:52:5) Function filter> could not be applied to arguments:
Domains:
[[[(U (Seqable Any) Counted nil) -> Bool] -> Any] -> Any :filters {:then (is (Option (clojure.lang.Seqable [(U nil (Seqable Any) clojure.lang.Counted) -> Bool])) 0), :else tt}] (Option (clojure.lang.Seqable [[(U (Seqable Any) Counted nil) -> Bool] -> Any]))
[[[(U (Seqable Any) Counted nil) -> Bool] -> Any] -> Any :filters {:then (! (Option (clojure.lang.Seqable [(U nil (Seqable Any) clojure.lang.Counted) -> Bool])) 0), :else tt}] (Option (clojure.lang.Seqable [[(U (Seqable Any) Counted nil) -> Bool] -> Any]))
[[[(U (Seqable Any) Counted nil) -> Bool] -> Any] -> Any] (Option (clojure.lang.Seqable [[(U (Seqable Any) Counted nil) -> Bool] -> Any]))
Arguments:
[(U nil (Seqable Any) clojure.lang.Counted) -> Bool] (clojure.lang.LazySeq Any)
Ranges:
(ASeq (Option (clojure.lang.Seqable [(U nil (Seqable Any) clojure.lang.Counted) -> Bool])))
(ASeq (I [[(U (Seqable Any) Counted nil) -> Bool] -> Any] (Not (Option (clojure.lang.Seqable [(U nil (Seqable Any) clojure.lang.Counted) -> Bool])))))
(ASeq [[(U (Seqable Any) Counted nil) -> Bool] -> Any])
ExceptionInfo Type Checker: Found 1 error clojure.core/ex-info (core.clj:4566)
原始功能:
(defn first-two [player]
(let [couples (concat adjacents opposite-corners opposite-sides)]
(first (filter #(= (count %) 2)
(for [pair couples]
(clojure.set/intersection pair @player))))))
這是for循環的返回類型。再次感謝,男士。 – kurofune 2014-12-12 04:34:09