Last active
August 29, 2017 19:03
-
-
Save lambdahands/37d7f341019fff59a0be0e5546c1354d to your computer and use it in GitHub Desktop.
Clojure behavioral dispatch psuedo-code
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
(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