我可以使用Clojure的派生创建我的defrecord类类型的层次结构吗? [英] Can I use Clojure's derive to create a hierarchy of my defrecord class types?

查看:125
本文介绍了我可以使用Clojure的派生创建我的defrecord类类型的层次结构吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做类似的操作:

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

(derive Person Base)
(derive Animal Base)

(isa? Animal Person)

这是否可能?

我以后意识到这是不可能的,所以我做这样的事情:

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?

推荐答案

否。记录是Java类。由于多方法页面指出:

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


您可以也使用一个类作为子
(但不是父类,唯一的方法是
通过Java继承来使某个类的子类为
)。

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).

您不能扩展具有记录的类,但您可以实现接口。使用接口在Java类层次结构中播放,您可能能够使某些工作。

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.

这篇关于我可以使用Clojure的派生创建我的defrecord类类型的层次结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆