Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case clause doesn't match record type

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)

In case you're wondering, yes, the case expression and the test-constant are equal:

(= (type rec) Rec)
=> true
like image 795
Ben Kovitz Avatar asked Nov 29 '25 16:11

Ben Kovitz


1 Answers

Rec is not a compile-time literal. Quoting from https://clojuredocs.org/clojure.core/case:

All manner of constant expressions are acceptable in case, including numbers, strings, symbols, keywords, and (Clojure) composites thereof.

Alternatives:

(cond 
  (= (type rec) Rec) :YES)
;;=> :YES
(condp = (type rec) 
  Rec :YES)
;;=> :YES
like image 58
Chris Murphy Avatar answered Dec 02 '25 06:12

Chris Murphy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!