A small Clojure utility library for defining abstract functions whose implementation will be provided elsewhere (perhaps externally), or for defining methods. Useful for dependency injection and otherwise nicely decoupling different modules of your project from one another.
This library provides a function,
external-fn, that you can use to create an externally-defined function. This resulting object works exactly like a normal Clojure function, but will throw an Exception if invoked before an instance has been provided:
(require '[external-fn.core :as external-fn])
(def f (external-fn/external-fn))
(f) ;-> AbstractMethodError
You can provide an implementation with
;; provide an implementation
(external-fn/provide-impl! f (constantly :ok))
;; now function will work as expected
(f) ;-> :ok
This library also provides a
defexternal macro to define an external function with almost the exact same syntax as
defn; the usual nice-to-have metadata like
:arglists is provided as well.
"You can add docstrings"
(external-fn/provide-impl! my-fn (fn [x] (+ x x)))
(my-fn 2) ;-> 4