Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Clojure behavioral dispatch psuedo-code
(defprotocol ISpeak
(hello [this]))
(defbehavior ::philip
(hello [_]
(println "Hello, my name is Philip!")))
(defdispatch Person [this]
[(:name this) (:age this)])
(defbehavior Person [::philip 26]
(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
You can’t perform that action at this time.