After a couple of days of hacking it's clear that Clojure can support OO
structures really, really, well.  I've whipped up a little thing, jokingly
called CLJOS.  I'm curious to know what people think.  It's a fairly simple
affair combining structs, hierarchies, and some helper functions.
It's my first attempt at Clojure programming so I'm sure it could use some
cleanup, but it works.  Of course open to any thoughts improvements etc.  In
particular I'm using refs to deal with changes to the hierarchy and the
class initializers.

You can compose structs, here's a very nonsensical example:

;; multiple inheritance
(defclass beaver [object]
  (:tail true))
(defclass duck [object]
  (:bill true))
(defclass platypus [beaver duck]
  (:marsupial true))

(def aplatypus (make-instance platypus))
(isaa? aplatypus ::duck) ;; yield true
(isaa? aplatypus ::beaver) ;; yields true

isaa? takes an instance and matches it against a fully namespaced keyword.

It supports default values for keys, they are overridden according to the
class hierarchy.

(defclass shape [object]
  (:position [0 0]))

(defclass circle [shape]
  (:radius 10))

(make-instance circle :raduis 20]) ;; creates struct -> {:position [0 0],
:tag :cljos-example/circle, :radius 20}

And it support a simple system for calling multimethods up the inheritance
chain simply by re-assigning the :tag.

(defmulti area :tag)

(defmethod area ::shape [s]
  (print "Abstract method!\n"))

(defmethod area ::rect [r]
  (print "Area of rect\n")
  (area (super r))
  (* (:width r) (:height r)))

http://github.com/swannodette/clojure-stuff/blob/d5df8ad710fdb139d78b95a41ab914c30db53d6d/cljos.clj

Of course it blows my mind that this can be done in less than 100 lines of
code (Clojure is amazing!). It seems to me a decent way to deal with OO
style coding if you want to stick with pure clojure and don't need the Java
interop of gen-class.

--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to