Skip to content

Instantly share code, notes, and snippets.

@lambdahands
Last active August 29, 2017 19:03
Show Gist options
  • Save lambdahands/37d7f341019fff59a0be0e5546c1354d to your computer and use it in GitHub Desktop.
Save lambdahands/37d7f341019fff59a0be0e5546c1354d to your computer and use it in GitHub Desktop.
Clojure behavioral dispatch psuedo-code
(defprotocol ISpeak
(hello [this]))
(defbehavior ::philip
ISpeak
(hello [_]
(println "Hello, my name is Philip!")))
(defdispatch Person [this]
[(:name this) (:age this)])
(defbehavior Person [::philip 26]
ISpeak
(hello [_]
(println "Hello, my name is Philip! I'm 26.")))
(hello ::philip)
;; => Hello, my name is Philip!
(hello {:name ::philip :age 26})
;; => Hello, my name is Philip! I'm 26.
(hello nil)
;; => Uh-oh! What to do here? Maybe allow a :default key in defbehavior?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment