Ben Kovitz
Ben Kovitz

Reputation: 5020

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

Upvotes: 3

Views: 995

Answers (1)

Chris Murphy
Chris Murphy

Reputation: 6509

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

Upvotes: 4

Related Questions