On Dec 12, 9:21 am, Joost <jo...@zeekat.nl> wrote:
> * Inheritance is overrated. it tends to be abused as a way to share
> implementation details more than anything else. All the points I made
> about interfaces also apply to inheritance. See also traits, for a way
> to really do reuse:http://scg.unibe.ch/research/traits

I haven't read all the literature on traits, but I've begun using a
pattern in Clojure that resembles the concept. This is a pseudocode
sketch (don't try to execute), but I hope gets the pattern across.

(def trait-a {
  :do-something (fn do-something [applied-to & params]
    (using applied-to (mutate params)))
  })

(defn apply-trait [trait apply-to]
  (into {} (map
    (fn make-applied-trait-method
      [fn-symbol trait-fn]
      [fn-symbol (partial trait-fn apply-to)])
    trait)))

You'd use this like:

(let [
  trait-a-data (struct-map struct-i-use
    :datafield1 1 :datafield2 2)
  specific-fns (apply-trait trait-a trait-a-data)
  ]
  ((specific-fns :do-something) to-something))

Have other people done similar things, or have an improvement?

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

Reply via email to