开发者

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

开发者 https://www.devze.com 2023-02-02 00:33 出处:网络
I would l开发者_StackOverflowike to do something like: (defrecord Base []) (defrecord Person []) (defrecord Animal [])

I would l开发者_StackOverflowike 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消