Access vars from another clojure namespace?

In my main namespace, I have a top level var called "settings" that initializes to empty {}.

My -main fn sets the contents of the settings using def and conj based on some command line arguments (different database hosts for production / development, etc.).

I am trying to access the contents of this map from another namespace in order to pull out some settings. When I try to compile the lane in uberjar, I get the trace "There are no such var: lb / settings".

What am I missing? Is there a more idiomatic way to handle applications like these? Is it safe to use "def" inside -main, like me, or should I use atom or ref to make it thread safe?

Thank!

(ns com.domain.main
  (:use com.domain.some-other-namespace.core)
  (:gen-class))

(def settings {})

(defn -main [& args]
  (with-command-line-args... ;set devel? based on args
    (if (true? devel?)
    (def settings (conj settings {:mongodb {:host "127.0.0.1"}
                      :memcached {:host "127.0.0.1"}}))
    (def settings (conj settings {:mongodb {:host "PRODUCTION_IP"}
                      :memcached {:host "PRODUCTION_IP"}})))


;file2.clj
(ns com.domain.some-other-namespace.core
  (:require [main :as lb]
  ...)

;configure MongoDB
(congo/mongo!
  :db "dbname" :host (:host (mongodb lb/settings))))
...
+3
source share
2 answers

Ok, I found a problem. It looks like it was a circular link. I was ": require" ing com.domain.some-other-namespace.core from com.domain.main. Since "require" is called before (def settings {}) in com.domain.main, var does not yet exist when another namespace is compiled ...

I moved the settings map to a separate namespace (named settings naturally) and changed it from Var to Atom to be safe. Seems to work great now!

+4
source

A few things to check:

usually clojure namespaces have at least one. in them project.mainI think that leiningen may depend on this.

check the class folder to make sure the class files of the main and some other namespace are compiled.

0
source

Source: https://habr.com/ru/post/1783760/


All Articles