Liking cljdoc? Tell your friends :D

Downloads Dependencies Status Circle CI License cljdoc badge

Clojars Project

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-impl!:

;; 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.

(external-fn/defexternal my-fn
  "You can add docstrings"
  {:can-add-attribute-maps? true}
  [x])

(external-fn/provide-impl! my-fn (fn [x] (+ x x)))

(my-fn 2) ;-> 4

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close