views:

291

answers:

1

Yesterday, Rich pulled the 'new' branch of Clojure into master. We are now embracing the beauty that is deftype and defprotocol. Of course, I, coming from Haskell, am very tempted to define types like I would in Haskell, which would be for virtually everything short of a throwaway tuple, but I don't think it works like that in Clojure world ;). In the Common Mistakes thread for Clojure, one guy mentioned that overusing structs was a mistake he made when he first started, coming from OOP. Since deftypes seem to be similar to structs, I was wondering if the same thing applies there.

So, my question is: when is it a good time to use deftype?

+6  A: 

One area deftype shines is performance. Methods of protocols are very fast on a deftype. Also deftype may have primitive fields, so no boxing anymore when crunching numbers...

Another area might be Java interoperation, since deftype can implement interfaces (and if AOT compiled) have a named class.

In general is the basic idea to define abstractions with protocols and to implement them with deftype.

Rich details his motivation here: http://www.assembla.com/wiki/show/clojure/Datatypes

kotarak
So it would kind of be like Haskell, in that protocols are like typeclasses and datatypes are like... well datatypes. :p
Rayne