namespaces - Clojure - how to let the library users choose which printing function to use, to display data structures? -
I am writing a small debugging library and I want to let users choose how to display data structures. I was imagining that users may need it like this:
(ns some-user-namespace (: [requires clojure.pprint] [my.library: With-args {print- Is this possible, and if not, how can I solve this problem
It is not possible to do this in such a way that if you actually offer such an installation, you can call the user The configuration function can be provided:
(ns some namespace (requires: [my.library]) ! This is an idiomatic method of pointing out that it is creating some side effect. In your library it can appear:
(ns my.library) (defconfig (atom {: printer println})) (defn print-with [New-printer] (swap! Config assoc: printer new-printer)) (my-lib -print [foo] defin ((:: printer @ config) foo)) edit : a solution For those who do not require global, unstable state, you can use dynamic binding. Lib:
(ns my.library) (def ^: dynamic * printer * println) (my-lib-print [foo] (* printer * foo) Cancel) Usage:
(binding [my.library/ * printer * clojure.pprint / pprint] (my.library / my- Lib-print {hello "world"}))
These are only for two types of external, relevant configurations which I can think of is the pure high order function :
(my-lib-print [printer foo] (printer fu) defn)
Comments
Post a Comment