开发者

What is the Clojure equivalent of a "public static final" constant in Java

开发者 https://www.devze.com 2023-01-08 07:33 出处:网络
I\'m writing some Cl开发者_如何学编程ojure code that depends upon a number of constants. They will be used within tight inner loops, so it\'s important that they will be used and optimised as efficie

I'm writing some Cl开发者_如何学编程ojure code that depends upon a number of constants.

They will be used within tight inner loops, so it's important that they will be used and optimised as efficiently as possible by the Clojure compiler+JVM combination. I would normally used a "public static final" constant in Java for the same purpose.

What is the best way to declare these?


I think def-ing things in the global namespace is about as close as you can come.


I believe Clojure 1.3 (or maybe 1.4) allows you to put a ^:constant tag on a def, signifying to the compiler that this should be a constant and all references should be resolved at compile-time.

Edit

Apparently it's Clojure 1.3, and it's ^:const, not ^:constant. See How does Clojure ^:const work? for a summary.


There's no defconst, so just using a global def is idiomatic; as far as optimisation is concerned, the JIT will make things fast.


If really, really, really want the constant in place (I believe, the JIT will notice the value being constant and do the right thing, though), you can use a macro.

(defmacro my-constant [] 5)

This is rather ugly, but performance critical code will always be ugly, I guess.

(do-stuff (my-constant) in-place)

Pay care what you put into the macro, though! I wouldn't this for more than some literal constants. In particular not objects.


If just using def is not fast enough, you could try creating a let bound alias before entering your tight loop to avoid going through a var each time.


as said above use def or atom, remember, data is immutable, so if you declare some constants in a list, they don't change.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号