clojure - 我可以使用 Clojure 的派生来创建我的 defrecord 类类型的层次结构吗?

标签 clojure multimethod

我想做类似的事情:

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

(derive Person Base)
(derive Animal Base)

(isa? Animal Person)

这可能吗?

更新:

我已经意识到这是不可能的,所以我正在做这样的事情:
(defmulti type class)
(defmethod type Base [_] ::base )
(defmethod type Animal [_] ::animal )
(defmethod type Person [_] ::person )

这有意义还是有更好的方法?

最佳答案

不,记录是 Java 类。作为multimethods页面状态:

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



你不能用记录来扩展类,但你可以implement interfaces .使用接口(interface)在 Java 类层次结构中发挥作用,您也许可以使某些事情起作用。

关于clojure - 我可以使用 Clojure 的派生来创建我的 defrecord 类类型的层次结构吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4586608/

相关文章:

clojure - 如何在配置文件/项目配置的另一部分中使用 profiles.clj 中的值?

java - 在 clojure 中构建布隆过滤器时要使用哪些散列技术?

clojure - 在 Clojure Bron-Kerbosch 实现中正确使用集合

Clojure:不同类类型上的 defmulti

使用 isinstance 的 Python 多方法

html - 在 hiccup 中更改一个 div 颜色

clojure - clojure 中交错的扩展

haskell - Clojure 中的协议(protocol)和多方法在多态性方面不如 Haskell 中的类型类强大的原因是什么?

julia - Julia 是如何实现多方法的?

role - 为什么 Perl 6 Str 扮演 Positional 角色,如何更改 []?