Skip to content

Commit

Permalink
defonceinterface
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Sep 13, 2019
1 parent 19ecca4 commit 0e65c54
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/methodical/interface.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
(ns methodical.interface
(:refer-clojure :exclude [isa? prefers prefer-method]))

(definterface MethodCombination
(defmacro ^:private defonceinterface [name & body]
(let [class-name (clojure.string/replace (str *ns* "." name) #"\-" "_")
exists (try
(Class/forName class-name)
true
(catch Exception _
false))]
(if exists
`(do
(import ~(symbol class-name))
nil)
`(definterface ~name ~@body))))

(defonceinterface MethodCombination
(allowedQualifiers [])
(combineMethods [primary-methods aux-methods])
(transformFnTail [qualifier fn-tail]))
Expand Down Expand Up @@ -31,7 +44,7 @@
[^MethodCombination method-combination qualifier fn-tail]
(.transformFnTail method-combination qualifier fn-tail))

(definterface MethodTable
(defonceinterface MethodTable
(primaryMethods [])
(auxMethods [])
(addPrimaryMethod [dispatch-value f])
Expand Down Expand Up @@ -75,7 +88,7 @@
[^MethodTable method-table qualifier dispatch-val method]
(.removeAuxMethod method-table qualifier dispatch-val method))

(definterface Dispatcher
(defonceinterface Dispatcher
(dispatchValue [])
(dispatchValue [a])
(dispatchValue [a b])
Expand Down Expand Up @@ -127,7 +140,7 @@
[^Dispatcher dispatcher dispatch-val-x dispatch-val-y]
(.preferMethod dispatcher dispatch-val-x dispatch-val-y))

(definterface MultiFnImpl
(defonceinterface MultiFnImpl
(^methodical.interface.MethodCombination methodCombination [])
(^methodical.interface.Dispatcher dispatcher [])
(^methodical.interface.MultiFnImpl withDispatcher [new-dispatcher])
Expand Down Expand Up @@ -168,7 +181,7 @@
[^MultiFnImpl multifn dispatch-value]
(.effectiveMethod multifn dispatch-value))

(definterface Cache
(defonceinterface Cache
(cachedMethod [dispatch-value])
(cacheMethodBang [dispatch-value method])
(clearCacheBang [])
Expand Down

0 comments on commit 0e65c54

Please sign in to comment.