开发者

How does CLOS relate to Clojure's type and record features?

开发者 https://www.devze.com 2023-02-25 08:14 出处:网络
I have been working recently with Clojure\'s types and records rece开发者_运维问答ntly, and I was wondering if these are very new concepts, or are they inspired by the Common Lisp Object System?I beli

I have been working recently with Clojure's types and records rece开发者_运维问答ntly, and I was wondering if these are very new concepts, or are they inspired by the Common Lisp Object System?


I believe they are pretty much new innovations within Clojure.

CLOS is a fairly complex, fully featured object oriented system. It features various OOP techniques you often hear mentioned and which exist in other object oriented languages - multiple inheritance, dynamic dispatch, generic functions etc.

Clojure takes a different approach - types and records are much simpler than full OOP and are not intended to constitute a full OOP system. Rather, I understand that the design is motivated by:

  • The belief that many OOP techniques are actually harmful when building large systems - implementation inheritance for example
  • The opportunity to get maximum performance (i.e. the same as Java) for the most common case of polymorphism (i.e. single dispatch on type)
  • The desire to solve the "expression problem", which you can do in Clojure using deftype / defrecord together with protocols
  • The intent to make all records/types immutable in order to fit with the rest of Clojure's philosophy

If you want a traditional object oriented system like CLOS, it would be possible to build this in Clojure on on top of types and records. You can also use Java-style object orientation directly within Clojure. However I believe it's not usually recommended by Clojure experts - Clojure usually offers you different or better ways to solve the same problems.

In general - Clojure tends to provide you with "simple" tools that you can compose to solve the problem at hand, rather than prescribing a complex framework at any point. It's an interesting philosophy discussed at some length in this video by Stuart Halloway.

0

精彩评论

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