views:

176

answers:

1

Clojure structs can be arbitrarily extended, adding new fields.

Is it possible to extend types (created using deftype) in a similar way?

EDIT: For the benefit future visitors, as Brian pointed out below, this feature is subject to change.

+6  A: 

Actually you can treat types as maps, you just need to extend clojure.lang.IPersistentMap (an implementation is magically supplied).

(deftype A [a b]
  clojure.lang.IPersistentMap)
(A 1 2) ;; => #:A{:a 1, :b 2}
(assoc (A 1 2) :c 3) ;; => #:A{:a 1, :b 2, :c 3}
Mike Douglas
I like magically supplied things. Thanks for the answer.
Rob Lachlan