Providing Global Variables

I need to provide custom vars for my users, such as the warning-on-reflection provided by clojure AFAIK, they are not defined on the clojure side, so we can install them.

The problem is that my vars (all configuration items) are used in many hard loops, so I don’t want them to refer to them, because they MAY be installed when the application starts, and this will not change at runtime, they will be read maybe millions of times, so making them refs seems like a waste of resources.

So the question is, can I define custom vars in my case?

+4
source share
3 answers

warning-on-reflection is just var, albeit defined in Clojure Java code, not core.clj. However, apart from where it is defined, there is nothing special in the warning of reflection; it behaves exactly like any other var.

If you do not want to use vars, you may need to approach your problem from a different perspective than using global variables. In functional programming, it is customary to pass all the necessary values ​​to a function, rather than relying on global variables. Perhaps it is time for you to consider this approach.

+1
source

If you want to set up a global low-cost state that is visible to all threads and does not need any STM transactions to manage the mutation, I would recommend just using atoms:

(def some-global-value (atom 1)) 

Reading and writing to atoms is extremely low.

+2
source

You can create a local binding using let , so the parameters can be dynamic and you will have quick access. The only thing is that if someone changes the parameter during the loop, the loop will not pick it up (IMO this should be the desired behavior anyway).

+1
source

All Articles