Can I use Clojure's derive to create a hierarchy of my defrecord class types?

青春壹個敷衍的年華 提交于 2019-12-10 13:32:02

问题


I would like to do something like:

(defrecord Base [])
(defrecord Person [])
(defrecord Animal [])

(derive Person Base)
(derive Animal Base)

(isa? Animal Person)

Is this possible?

Update:

I've since realized that this is not possible so I am doing something like this:

(defmulti type class)
(defmethod type Base [_] ::base )
(defmethod type Animal [_] ::animal )
(defmethod type Person [_] ::person )

Does this make sense or is there a better way?


回答1:


No. Records are Java classes. As the multimethods page states:

You can also use a class as the child (but not the parent, the only way to make something the child of a class is via Java inheritance).

You can't extend classes with records but you can implement interfaces. Using interfaces to play in the Java class hierarchy, you might be able to make something work.



来源:https://stackoverflow.com/questions/4586608/can-i-use-clojures-derive-to-create-a-hierarchy-of-my-defrecord-class-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!