concept deftype in category clojure

This is an excerpt from Manning's book Clojure in Action, Second Edition.
The mechanisms we’re going to talk about create underlying classes on the host platform (namely Java today, but it could be others tomorrow). This means that they share the same performance as the native version of such data types, as well as the same polymorphic capabilities supported by the host. We’ll first look at defrecord, followed by deftype. We’ll close the section with a look at reify.

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.
With direct method calls, proxy, gen-class, gen-interface (see chapter 10), reify, definterface, deftype, and defrecord (see section 9.3), Clojure works hard to provide a bevy of interoperability options, all in the name of helping you get your job done. Practicality is important to Clojure, but many other languages are practical as well. You’ll start to see some ways that Clojure sets itself apart by looking at how it avoids muddles.
It’s important to note that when you define a defrecord and a deftype, corresponding classes are generated. These classes are automatically imported into the same namespace where the defrecord and deftype declarations occur, but not in any other namespace. Instead, you must explicitly import defrecord and deftype classes using the import function or :import namespace declaration:
(ns my-cool-ns (:import joy.udp.TreeNode))Loading a namespace via :require or :use isn’t enough to import defrecord and deftype classes.
This is because record types are maps and implement everything maps should—seq along with assoc, dissoc, get, and so forth. Because these are provided for you, you can’t implement them again yourself, and thus the preceding exception. For the rare case where you’re building your own data structure instead of creating application-level record types, Clojure provides a lower-level deftype construct that’s similar to defrecord but doesn’t implement anything at all, so implementing seq won’t conflict with anything:
With all that in mind, the following listing is a final implementation of TreeNode using deftype, which lets you implement not only ISeq so that you can use seq instead of xseq, but also IPersistentStack so you can use peek, pop, and conj as well as the fixo- versions.
Listing 9.6. Implementing map interfaces with deftype
(deftype TreeNode [val l r] FIXO #1 (fixo-push [_ v] (if (< v val) (TreeNode. val (fixo-push l v) r) (TreeNode. val l (fixo-push r v)))) (fixo-peek [_] (if l (fixo-peek l) #2 val)) (fixo-pop [_] (if l (TreeNode. val (fixo-pop l) r) r)) clojure.lang.IPersistentStack #3 (cons [this v] (fixo-push this v)) (peek [this] (fixo-peek this)) (pop [this] (fixo-pop this)) clojure.lang.Seqable (seq [t] (concat (seq l) [val] (seq r)))) (extend-type nil FIXO (fixo-push [t v] (TreeNode. v nil nil))) #4 (def sample-tree2 (into (TreeNode. 3 nil nil) [5 2 4 6])) (seq sample-tree2) ;=> (2 3 4 5 6) #1 - Implement FIXO methods inline #2 - Call a method instead of using recur #3 - Implement interfaces #4 - Redefine to use the new TreeNode !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!A final note about deftype: it’s the one mechanism by which Clojure lets you create classes with volatile and mutable fields. We won’t go into it here because using such classes is almost never the right solution. Only when you’ve learned how Clojure approaches identity and state, how to use reference types, what it means for a field to be volatile, and all the pitfalls related to that, should you even consider creating classes with mutable fields. By then, you’ll have no problem understanding the official docs for deftype, and you won’t need any help from us.