本文介绍了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 不是编译时文字.引用自 clojuredocs/clojure.core/case :
Rec is not a compile-time literal. Quoting from clojuredocs/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子句与记录类型不匹配
发布评论