Hi is it possible to make a conditional def in one namespace visible in another namespace
consider I have 3 namespaces debug, if-def-demo and main as below 1.) debug namespace
(ns debug)
(def enabled? true)
2.) if-def-demo namespace
(ns if-def-demo)
(def mseq [ "a" "b" "c"])
(if (debug/enabled?)
(def my-var mseq))
3.) in main namespace
(ns main)
(println if-def-demo/my-var)
if-def-demo/my-var is not visible in main namespace
The fact that a static analysis tool like Cursive cannot find it is usually a sign that things are getting a bit too complex.
I personally like to use a combination of (defonce ...) and atoms for such cases instead of conditional (def ) or conditional-anything…
(ns debug)
(defonce enabled? (atom true))
(ns if-def-demo)
(def mseq ["a" "b" "c"])
(defonce my-atom (atom nil))
;; also ideally hide this inside a fn, (defn init [ ] ...) or similar
;; top-level side effects can create complex hard-to-debug behavior during loading or compilation phases
(if @debug/enabled?
(reset! my-atom mseq))
If you need something more… “proper” for managing stateful stuff like that, this is a good choice https://github.com/stuartsierra/component
But I still end up using the defonce + atom simple solution more often than not