Skip to content

Instantly share code, notes, and snippets.

@runexec
Last active June 17, 2020 03:47
Show Gist options
  • Select an option

  • Save runexec/786b92b97b5a3a0ffac7 to your computer and use it in GitHub Desktop.

Select an option

Save runexec/786b92b97b5a3a0ffac7 to your computer and use it in GitHub Desktop.

Revisions

  1. runexec revised this gist Nov 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Clojure Does Objects Better.clj.md
    Original 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.
    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])
  2. runexec revised this gist Nov 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Clojure Does Objects Better.clj.md
    Original 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 a protocol.
    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])
  3. runexec created this gist Nov 27, 2014.
    45 changes: 45 additions & 0 deletions Clojure Does Objects Better.clj.md
    Original 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!!!!"