I always load a namespace not-really-constants
. It contains, among other definitions:
(def foo 0.05)
Another namespace contains
(ns other-ns
(require [not-really-constants :as cn]))
(defn bar [x] (* x cn/foo))
However, sometimes I want foo
to have a different value. So I load a namespace containing
(in-ns 'not-really-constants)
(def foo 0.025)
And then I call other-ns/bar
in the place where it would normally be called. bar
then uses the new value of foo
, 0.025, rather than the original 0.05.
I'm not sure whether to be suprised about this behavior. I think of redefining using def
as a handy convenience in the repl, but as something that's one is not supposed to do in normal code. However, I'm not sure why. Redefining foo
in this way seems to work without trouble running everything from the commandline using lein run
, loading files via require
.
So, my question is: What are the dangers or other drawbacks of this practice--i.e. of redefining a variable using def
?
(BTW I don't add ^:dynamic
to the definition of foo
because I don't need thread-local bindings; the new value of foo
should be used everywhere. If I add ^:const
to the definition of foo
, bar
uses the original 0.05 value despite the fact that I redefined foo
, as bar
should in that case.)