Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a mix of three different factors:</p> <ol> <li>The particular type system of the jvm</li> <li>The need for slightly different semantics for different use cases when defining types</li> <li>The fact that some of these were developed earlier, and some later, as the language has evolved.</li> </ol> <p>So first, let's consider what these do. <strong>deftype</strong> and <strong>gen-class</strong> are similar in that they both define a named class for ahead-of-time compilation. Gen-class came first, followed by deftype in clojure 1.2. Deftype is preferred, and has better performance characteristics, but is more restrictive. A deftype class can conform to an interface, but cannot inherit from another class.</p> <p><strong>Reify</strong> and <strong>proxy</strong> are both used to dynamically create an instance of an anonymous class at runtime. Proxy came first, reify came along with deftype and defrecord in clojure 1.2. Reify is preferred, just as deftype is, where the semantics are not too restrictive.</p> <p>That leaves the question of why both deftype and defrecord, since they appeared at the same time, and have a similar role. For most purposes, we will want to use defrecord: it has all the various clojure goodness that we know and love, sequability and so forth. Deftype is intended for use as a low level building block for the implementation of other datastructures. It doesn't include the regular clojure interfaces, but it does have the option of mutable fields (though this isn't the default).</p> <p>For further reading check out:</p> <p><a href="http://clojure.org/datatypes" rel="noreferrer">The clojure.org datatypes page</a></p> <p><a href="http://groups.google.com/group/clojure/msg/330c230e8dc857a9" rel="noreferrer">The google group thread where deftype and reify were introduced</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload