Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Move Describable to methodical.interface #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/methodical/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
methodical.interface
methodical.macros
methodical.util
methodical.util.describe
methodical.util.dispatch
methodical.util.trace
[potemkin :as p]))
Expand All @@ -16,7 +15,6 @@
methodical.impl/keep-me
methodical.interface/keep-me
methodical.macros/keep-me
methodical.util.describe/keep-me
methodical.util.dispatch/keep-me
methodical.util.trace/keep-me
methodical.util/keep-me)
Expand Down Expand Up @@ -48,7 +46,8 @@
with-dispatcher
method-table
with-method-table
effective-method]
effective-method
describe]

[methodical.impl
;; method combinations
Expand Down Expand Up @@ -125,8 +124,6 @@
unprefer-method!
with-prefers!]

[methodical.util.describe
describe]

[methodical.util.dispatch
dispatch-on-first-arg
Expand Down
49 changes: 35 additions & 14 deletions src/methodical/impl/cache/simple.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,62 @@
offers no facilities to deduplicate identical methods for the same dispatch value. This behaves similarly to the
caching mechanism in vanilla Clojure."
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.interface]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
[clojure.core.protocols :as clojure.protocols]
[methodical.interface :as i]
[pretty.core :as pretty])
(:import
(methodical.interface Cache)))
(methodical.interface
Cache)))


(set! *warn-on-reflection* true)

(comment methodical.interface/keep-me)

(deftype SimpleCache [atomm]

(deftype SimpleCache
[atomm]

pretty/PrettyPrintable
(pretty [_]

(pretty
[_]
'(simple-cache))


Cache
(cached-method [_ dispatch-value]

(cached-method
[_ dispatch-value]
(get @atomm dispatch-value))

(cache-method! [_ dispatch-value method]

(cache-method!
[_ dispatch-value method]
(swap! atomm assoc dispatch-value method))

(clear-cache! [this]

(clear-cache!
[this]
(reset! atomm {})
this)

(empty-copy [_]

(empty-copy
[_]
(SimpleCache. (atom {})))


clojure.protocols/Datafiable
(datafy [this]

(datafy
[this]
{:class (class this)
:cache @atomm})

describe/Describable
(describe [this]

i/Describable

(describe
[this]
(format "It caches methods using a [[%s]]." (.getCanonicalName (class this)))))
3 changes: 1 addition & 2 deletions src/methodical/impl/cache/watching.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
[clojure.core.protocols :as clojure.protocols]
[clojure.datafy :as datafy]
[methodical.interface :as i]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(java.lang.ref WeakReference)
Expand Down Expand Up @@ -55,7 +54,7 @@
:cache (datafy/datafy cache)
:refs refs})

describe/Describable
i/Describable
(describe [this]
(format "It caches methods using a [[%s]]." (.getCanonicalName (class this)))))

Expand Down
5 changes: 2 additions & 3 deletions src/methodical/impl/combo/clojure.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
not at all. Like vanilla Clojure multimethods, this method combination only supports primary methods."
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.interface]
[methodical.util.describe :as describe]
[methodical.interface :as i]
[pretty.core :as pretty])
(:import
(methodical.interface MethodCombination)))
Expand Down Expand Up @@ -38,6 +37,6 @@
(datafy [this]
{:class (class this)})

describe/Describable
i/Describable
(describe [this]
(format "It uses the method combination [[%s]]." (.getCanonicalName (class this)))))
5 changes: 2 additions & 3 deletions src/methodical/impl/combo/clos.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.impl.combo.common :as combo.common]
[methodical.interface]
[methodical.util.describe :as describe]
[methodical.interface :as i]
[pretty.core :as pretty])
(:import
(methodical.interface MethodCombination)))
Expand Down Expand Up @@ -87,6 +86,6 @@
(datafy [this]
{:class (class this)})

describe/Describable
i/Describable
(describe [this]
(format "It uses the method combination [[%s]]." (.getCanonicalName (class this)))))
5 changes: 2 additions & 3 deletions src/methodical/impl/combo/operator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
[clojure.core.protocols :as clojure.protocols]
[clojure.spec.alpha :as s]
[methodical.impl.combo.common :as combo.common]
[methodical.interface]
[methodical.util.describe :as describe]
[methodical.interface :as i]
[pretty.core :as pretty])
(:import
(methodical.interface MethodCombination)))
Expand Down Expand Up @@ -202,7 +201,7 @@
{:class (class this)
:operator operator-name})

describe/Describable
i/Describable
(describe [this]
(format "It uses the method combination [[%s]]\nwith the operator `%s`."
(.getCanonicalName (class this))
Expand Down
5 changes: 2 additions & 3 deletions src/methodical/impl/combo/threaded.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.impl.combo.common :as combo.common]
[methodical.interface]
[methodical.util.describe :as describe]
[methodical.interface :as i]
[pretty.core :as pretty])
(:import
(methodical.interface MethodCombination)))
Expand Down Expand Up @@ -105,7 +104,7 @@
{:class (class this)
:threading-type threading-type})

describe/Describable
i/Describable
(describe [this]
(format "It uses the method combination [[%s]]\nwith the threading strategy `%s`."
(.getCanonicalName (class this))
Expand Down
3 changes: 1 addition & 2 deletions src/methodical/impl/dispatcher/everything.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[clojure.core.protocols :as clojure.protocols]
[methodical.impl.dispatcher.common :as dispatcher.common]
[methodical.interface :as i]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(methodical.interface Dispatcher)))
Expand Down Expand Up @@ -71,7 +70,7 @@
:hierarchy hierarchy-var
:prefs prefs})

describe/Describable
i/Describable
(describe [this]
(format "It uses the dispatcher [[%s]]\nwith hierarchy `%s`\nand prefs `%s`."
(.getCanonicalName (class this))
Expand Down
3 changes: 1 addition & 2 deletions src/methodical/impl/dispatcher/multi_default.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
[methodical.impl.dispatcher.common :as dispatcher.common]
[methodical.impl.dispatcher.standard :as dispatcher.standard]
[methodical.interface :as i]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(methodical.interface Dispatcher)))
Expand Down Expand Up @@ -198,7 +197,7 @@
:hierarchy hierarchy-var
:prefs prefs})

describe/Describable
i/Describable
(describe [this]
(format "It uses the dispatcher [[%s]]\nwith hierarchy `%s`\nand prefs `%s`.\n\nThe default value is `%s`."
(.getCanonicalName (class this))
Expand Down
3 changes: 1 addition & 2 deletions src/methodical/impl/dispatcher/standard.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
[clojure.core.protocols :as clojure.protocols]
[methodical.impl.dispatcher.common :as dispatcher.common]
[methodical.interface :as i]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
(:import
(methodical.interface Dispatcher)))
Expand Down Expand Up @@ -174,7 +173,7 @@
:hierarchy hierarchy-var
:prefs prefs})

describe/Describable
i/Describable
(describe [this]
(format "It uses the dispatcher [[%s]]\nwith hierarchy `%s`\nand prefs `%s`.\n\nThe default value is `%s`."
(.getCanonicalName (class this))
Expand Down
64 changes: 46 additions & 18 deletions src/methodical/impl/method_table/clojure.clj
Original file line number Diff line number Diff line change
@@ -1,61 +1,89 @@
(ns methodical.impl.method-table.clojure
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.impl.method-table.common :as method-table.common]
[methodical.interface]
[methodical.util.describe :as describe]
[pretty.core :as pretty])
[clojure.core.protocols :as clojure.protocols]
[methodical.impl.method-table.common :as method-table.common]
[methodical.interface :as i]
[pretty.core :as pretty])
(:import
(methodical.interface MethodTable)))
(methodical.interface
MethodTable)))


(set! *warn-on-reflection* true)

(comment methodical.interface/keep-me)

(deftype ClojureMethodTable [m]

(deftype ClojureMethodTable
[m]

pretty/PrettyPrintable
(pretty [_]

(pretty
[_]
(if (seq m)
(list 'clojure-method-table (count m) 'primary)
'(clojure-method-table)))


Object
(equals [_ another]

(equals
[_ another]
(and (instance? ClojureMethodTable another)
(= m (.m ^ClojureMethodTable another))))


MethodTable
(primary-methods [_]

(primary-methods
[_]
m)

(aux-methods [_]

(aux-methods
[_]
nil)

(add-primary-method [this dispatch-val method]

(add-primary-method
[this dispatch-val method]
(let [new-m (assoc m dispatch-val method)]
(if (= m new-m)
this
(ClojureMethodTable. new-m))))

(remove-primary-method [this dispatch-val]

(remove-primary-method
[this dispatch-val]
(let [new-m (dissoc m dispatch-val)]
(if (= m new-m)
this
(ClojureMethodTable. new-m))))

(add-aux-method [_ _ _ _]

(add-aux-method
[_ _ _ _]
(throw (UnsupportedOperationException. "Clojure-style multimethods do not support auxiliary methods.")))

(remove-aux-method [_ _ _ _]

(remove-aux-method
[_ _ _ _]
(throw (UnsupportedOperationException. "Clojure-style multimethods do not support auxiliary methods.")))


clojure.protocols/Datafiable
(datafy [this]

(datafy
[this]
{:class (class this)
:primary (method-table.common/datafy-primary-methods m)})

describe/Describable
(describe [this]

i/Describable

(describe
[this]
(format "It uses the method table [[%s]]. These primary methods are known:\n\n%s"
(.getCanonicalName (class this))
(method-table.common/describe-primary-methods m))))
4 changes: 2 additions & 2 deletions src/methodical/impl/method_table/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
(str/split-lines (describe-method f))))))

(defn describe-primary-methods
"Helper for [[methodical.util.describe/describe]]ing the primary methods in a method table."
"Helper for [[methodical.interface/describe]]ing the primary methods in a method table."
^String [dispatch-value->method]
(when (seq dispatch-value->method)
(format
Expand All @@ -71,7 +71,7 @@
(describe-method dispatch-value f))))))

(defn describe-aux-methods
"Helper for [[methodical.util.describe/describe]]ing the aux methods in a method table."
"Helper for [[methodical.interface/describe]]ing the aux methods in a method table."
^String [qualifier->dispatch-value->methods]
(when (seq qualifier->dispatch-value->methods)
(format
Expand Down
5 changes: 2 additions & 3 deletions src/methodical/impl/method_table/standard.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
(:require
[clojure.core.protocols :as clojure.protocols]
[methodical.impl.method-table.common :as method-table.common]
[methodical.interface]
[methodical.util.describe :as describe]
[methodical.interface :as i]
[pretty.core :as pretty])
(:import
(methodical.interface MethodTable)))
Expand Down Expand Up @@ -95,7 +94,7 @@
:primary (method-table.common/datafy-primary-methods primary)
:aux (method-table.common/datafy-aux-methods aux)})

describe/Describable
i/Describable
(describe [this]
(str (format "It uses the method table [[%s]]." (.getCanonicalName (class this)))
(method-table.common/describe-primary-methods primary)
Expand Down
Loading