我在 Clojurescript 中有两条记录,定义如下:
(defrecord Html [])
(defrecord Tree [])
我需要找出可以定义为这些记录中的任何一个的项目的类型,我该怎么做?
(def a (Html.))
我在 Clojurescript 中有两条记录,定义如下:
(defrecord Html [])
(defrecord Tree [])
我需要找出可以定义为这些记录中的任何一个的项目的类型,我该怎么做?
(def a (Html.))
(defrecord Html [])
(defrecord Tree [])
(= (type (->Html)) Html) ; true
(= (type (->Html)) Tree) ; false
(= (type (->Tree)) Html) ; false
(= (type (->Tree)) Tree) ; true
最好的、独立于主机的方法是:
(instance? Html a)
这适用于任何类型。
最后我像这样解决了它,它似乎有效:
(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)