Skip to content

Instantly share code, notes, and snippets.

@sbtourist
Created September 28, 2010 16:28
Show Gist options
  • Save sbtourist/601310 to your computer and use it in GitHub Desktop.
Save sbtourist/601310 to your computer and use it in GitHub Desktop.
;; Protocol defining output with external logic:
(defprotocol Outputter (output [self logic]))
;; Simple record:
(defrecord Person [name])
;; Two protocol definitions:
(deftype OutputterTrait [target] Outputter (output [self logic] (logic target)))
(deftype DummyTrait [] Outputter (output [self logic] (println "ignored external logic")))
;; Person instance:
(def joe (Person. "joe"))
;; Mixin function:
(defn mix [source mixins] (let [m (merge source mixins)] #(get m %1)))
;; Mixed joe:
(def mixed (mix joe {:outputter (OutputterTrait. joe) :dummy (DummyTrait)}))
;; Now using our mixed joe:
(output (mixed :outputter) #(println (:name %1)))
(output (mixed :dummy) #(println (:name %1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment