I would like to humbly ask...
What, "var or ref/atom/agent for constant values?", do I ask? Of course, I do use vars for constant values.
But I always wondered which one I should use when the values act like constants but they should be assigned in run-time, rather than in compile-time(when the codes are read).
For example, think some java properties written in user's config file. They should be assigned in run-time because the data is not in the codes. But they also should be defined before the data is read, since the other codes refer them.
In this case,
when do I use 'var's?:
when do I use ref/atom/agent?:
I don't know what I should use.
What do you use in those cases?
'var's? 'ref/atom/agent's? or even 'delay's?
Thanks in advance.
If all, or a group, of "constants", you are taking about, can be learned at a single point in time, let's call them "properties" instead.
And let's make a config that would "suck them in":
(defn resource [path]
(when path
(-> (Thread/currentThread) .getContextClassLoader (.getResource path))))
(def props
(edn/read-string
(slurp (io/file (resource (System/getProperty "your.conf"))))))
(defn conf [& path] ;; e.g. (conf :db :uri)
(get-in props (vec path)))
Your properties (e.g. "constants") file "your.conf" would be in the lines of:
{:db
{:uri "datomic:mem://dbname"
:other-property 42}
:rabbit
{:host "192.168.1.17"
:port 5672
:exchange "xyz-exchange"
:queue "zq"
:exchange.type "direct"
:vhost "/some-broker"
:username "user"
:password "strong"}}
Then later in your program/other namespaces you can access all this properties as:
(conf :db :uri) ;; will "constant"ly return "datomic:mem://dbname"
(conf :rabbit :host) ;; will "constant"ly return "192.168.1.17"
(conf :db :other-property) ;; will "constant"ly return 42
In "real life", the "props" var above would potentially check a "-D" path, have defaults, and handle exceptions, but it is a bit simplified to illustrate the point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With