Last active
June 17, 2020 03:47
-
-
Save runexec/786b92b97b5a3a0ffac7 to your computer and use it in GitHub Desktop.
Revisions
-
runexec revised this gist
Nov 27, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ A hopefully short and concise explanation as to how Clojure deals with Objects. If you already write Clojure, this isn't for you. You know what an Interface is if you write/read Java or PHP 5+. In Clojure it might be called defprotocol. user> (defprotocol IABC (also-oo [this]) -
runexec revised this gist
Nov 27, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ A hopefully short and concise explanation as to how Clojure deals with Objects. If you already write Clojure, this isn't for you. You know what an Interface is if you write/read Java or PHP 5+. In Clojure it might be called protocol. user> (defprotocol IABC (also-oo [this]) -
runexec created this gist
Nov 27, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ # Clojure does Objects Better A hopefully short and concise explanation as to how Clojure deals with Objects. If you already write Clojure, this isn't for you. You know what an Interface is if you write/read Java or PHP 5+. In Clojure it might be called a protocol. user> (defprotocol IABC (also-oo [this]) (another-fn [this x])) IABC You might be tempted to use the classic implements or extends keywords when defining a new Class. In Clojure it might be called defrecord. user> (defrecord ABC [name] ;; You can implement multiple Classes here IABC (also-oo [this] (str "This => " (:name this))) (another-fn [this x] (format "%s says: %s" (:name this) x))) user.ABC You might be tempted to use the classic new keyword to instantiate a new object. In Clojure it might be a simple decimal suffix added to the Class name so that it can be used as a predicate. user> (def a (ABC. "Roger")) #'user/a user> (another-fn a "oo programing") "Roger says: oo programing" user> (also-oo a) "This => Roger" Clojure can just do it better. user> (defmulti this-is-oo type) nil user> (defmethod this-is-oo ABC [this] (println (:name this))) #<MultiFn clojure.lang.MultiFn@8625d0e> user> (this-is-oo a) Roger user> (defmethod this-is-oo java.lang.String [this] "Got a string and not ABC!!!!") #<MultiFn clojure.lang.MultiFn@8625d0e> user> (this-is-oo "a") "Got a string and not ABC!!!!"