Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the Clojure core library use concrete derivation?

In the documentation of Clojure's type mechanisms, it is stated that

  • Concrete derivation is bad
    • you cannot derive datatypes from concrete classes, only interfaces

However, some of the core Clojure classes use concrete derivation (there are other examples, but these are the only cases in which the superclass is part of clojure.lang):

  • ARef extends AReference
  • Agent extends ARef
  • Atom extends ARef
  • Namespace extends AReference
  • Ref extends ARef
  • Var extends ARef

In addition, there are many abstract classes. However, there is no way to create the equivalent of an abstract class in Clojure, and to me, extension of an abstract class seems to have all the same drawbacks as regular concrete derivation.

Why is concrete derivation used here?

like image 249
Sam Estep Avatar asked Nov 17 '15 23:11

Sam Estep


1 Answers

I don't feel myself authoritative enough when speaking of the philosophy stuff, but here are my two cents.

The reason your quoted text appears there, is to warn on abuse of defrecord and deftype. Clojure does not encourage exposing records/types as API. Instead, one should expose interface whenever possible. After all, OO-language exposes classes and FP-language exposes functions/methods/interfaces.

On the other hand, you mentioned clojure's implementation itself uses abstract classes and inherits them. I would rather consider those as "the dirty works that some one has to do". JVM is designed such that it's most efficient on the primitives in the OO world. The gap between OO world virtual machine and a FP-world language has to be filled by somebody.

like image 130
Davyzhu Avatar answered Nov 14 '22 05:11

Davyzhu