Created
September 28, 2010 16:28
-
-
Save sbtourist/601310 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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