Reputation: 58816
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
Reputation: 31
The best, host-independent way to do it is:
(instance? Html a)
This will work for any type.
Upvotes: 2
Reputation: 1574
(defrecord Html [])
(defrecord Tree [])
(= (type (->Html)) Html) ; true
(= (type (->Html)) Tree) ; false
(= (type (->Tree)) Html) ; false
(= (type (->Tree)) Tree) ; true
Upvotes: 4
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