使用 Clojure Spec 检查缺点的类型一致性?

Use Clojure Spec to check type consistency on a cons?

如果我 运行 有借书人和书籍的图书馆:

(s/def ::brs (s/coll-of ::br/borrower))
(s/def ::bks (s/coll-of ::bk/book))

我想要一个通用函数来将一个项目添加到任一集合中:

(defn add-item [x xs]
  (if (some #{x} xs)
    xs
    (cons x xs)))

如何编写规范以确保我不能将图书添加给借阅者,反之亦然?

因为这个规格:

(s/fdef add-item
    :args (s/fspec :args (s/or :is-brs (s/and (s/cat :x ::br/borrower) (s/cat :xs ::brs))
                               :is-bks (s/and (s/cat :x ::bk/book) (s/cat :xs ::bks))))
    :ret (s/or :ret-brs ::brs
               :ret-bks ::bks))

不工作。 :-(

感谢您的帮助!

::brs::bks 使用 int/string 定义:

(s/def ::brs (s/coll-of int?))
(s/def ::bks (s/coll-of string?))

这应该有效:

(s/fdef add-item
        :args (s/or
                :brs (s/cat :x int? :xs ::brs)
                :bks (s/cat :x string? :xs ::bks))
        :ret (s/or :brs ::brs
                   :bks ::bks))
;; instrument function here
(add-item 1 [2])
=> (1 2)
(add-item "1" [2]) ;; throws exception