yazz.com
yazz.com

Reputation: 58816

How do I get the type of a record in Clojurescript?

I have two records in Clojurescript defined as follows:

(defrecord Html [])
(defrecord Tree [])

I need to find out the type of the item which can be defined as either of these records, how can I do this?

(def a (Html.))

Upvotes: 3

Views: 1719

Answers (3)

Outrovurt
Outrovurt

Reputation: 31

The best, host-independent way to do it is:

(instance? Html a)

This will work for any type.

Upvotes: 2

fgui
fgui

Reputation: 1574

(defrecord Html [])
(defrecord Tree [])

(= (type (->Html)) Html) ; true
(= (type (->Html)) Tree) ; false

(= (type (->Tree)) Html) ; false
(= (type (->Tree)) Tree) ; true

Upvotes: 4

yazz.com
yazz.com

Reputation: 58816

In the end I solved it like this and it seems to work:

(defrecord Html [])
(defrecord Tree [])



(defprotocol TypeInfo
  (gettype [this] nil)
)


(extend-type Html
  TypeInfo
  (gettype [this]  "Html")

)

(extend-type Tree
  TypeInfo
  (gettype [this]  "Tree")

)

(def a (Html.))


(gettype a)

(def b (Tree.))


(gettype b)

Upvotes: 1

Related Questions