case子句与记录类型不匹配

编程入门 行业动态 更新时间:2024-10-24 04:40:56
本文介绍了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子句与记录类型不匹配

本文发布于:2023-11-26 00:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631888.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   不匹配   类型   case

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!