case子句与记录类型不匹配 [英] case clause doesn't match record type
本文介绍了case子句与记录类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么 case
子句与记录类型不匹配?
Why doesn't the case
clause match the record type?
(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)
如果您想知道,是的,case表达式和test-constant相等:
In case you're wondering, yes, the case expression and the test-constant are equal:
(= (type rec) Rec)
=> true
推荐答案
Rec
不是编译时文字.引用自 https://clojuredocs.org/clojure.core/case :
Rec
is not a compile-time literal. Quoting from https://clojuredocs.org/clojure.core/case:
所有情况下的常量表达式都可以接受,包括数字,字符串,符号,关键字及其(Clojure)组合.
All manner of constant expressions are acceptable in case, including numbers, strings, symbols, keywords, and (Clojure) composites thereof.
替代品:
(cond
(= (type rec) Rec) :YES)
;;=> :YES
(condp = (type rec)
Rec :YES)
;;=> :YES
这篇关于case子句与记录类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文