On Jun 29, 2010, at 3:54 AM, Mark Engelberg wrote:
On Sun, Jun 27, 2010 at 7:18 AM, Rich Hickey <richhic...@gmail.com>
wrote:
There are no extension points built on protocols in Clojure yet.
Delivering
protocols is step one, re-architecting the core abstractions in
terms of
protocols is still to come.
Rich
So how hard it would it be right now, for example, to implement in
Clojure a deftype that responds to all the functions a sorted map
responds to?
I started looking through the source code to try to figure out what
interfaces I'd need to override, but it seemed like an overwhelming
mixture of Clojure and Java interfaces, and very difficult to get
right without the ability to use the existing abstract base classes
that are in place (AFn, ASeq, APersistentMap, etc.). For example, it
seems like to implement IFn, you'd need to manually write out cases
for the first twenty arities.
Would you recommend waiting until the core abstractions are
re-architected before tackling something like this, or is there an
easy way to do this I'm not seeing?
To start, you could look at the ancestors:
(ancestors (class (sorted-map)))
#{java.io.Serializable clojure.lang.ILookup clojure.lang.IMeta
java.lang.Iterable clojure.lang.Associative
java.util.concurrent.Callable clojure.lang.Reversible
clojure.lang.IPersistentCollection java.lang.Runnable
clojure.lang.APersistentMap clojure.lang.Sorted clojure.lang.Counted
clojure.lang.IObj java.lang.Object clojure.lang.IFn
clojure.lang.Seqable clojure.lang.IPersistentMap java.util.Map
clojure.lang.AFn}
You may or may not want to implement all of these.
To get an idea of what is involved, look at emit-defrecord in
core_deftype.clj.
Yes, in deftype-land we can't use the abstract bases.
I'm not sure it is every going to be made 'easy' to correctly support
so many abstractions, but it is certainly tractable, and not something
one needs to do often. emit-defrecord implements a dozen interfaces in
about 60 lines of heavy lifting (and as a macro to boot).
As to whether or not you want to wait, that's up to you. These things
are likely to change, but there are people who implement the
interfaces right now with great success.
Rich
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en