case子句与记录类型不匹配

时间:2017-10-12 02:00:53

标签: clojure switch-statement record

为什么case子句与记录类型不匹配?

(defrecord Rec [])
=> fargish.user.Rec

(def rec (->Rec))
=> #'fargish.user/rec

(case (type rec) Rec :YES)
=> IllegalArgumentException No matching clause: class fargish.user.Rec  fargish.user/eval25147 (form-init131856794870899934.clj:1)

如果您想知道,是的,案例表达式和测试常数是相等的:

(= (type rec) Rec)
=> true

1 个答案:

答案 0 :(得分:4)

Rec不是编译时文字。引自https://clojuredocs.org/clojure.core/case

  

所有类型的常量表达式都是可接受的,包括数字,字符串,符号,关键字和(Clojure)复合。

备选方案:

(cond 
  (= (type rec) Rec) :YES)
;;=> :YES
(condp = (type rec) 
  Rec :YES)
;;=> :YES
相关问题