3 ms·
> as long as you put the right type hints everywhere. I'm a novice Clojure programmer and haven't previously heard of this. Can you please elaborate?
by krat0sprakhar 10y ago
> as long as you put the right type hints everywhere.
I'm a novice Clojure programmer and haven't previously heard of this. Can you please elaborate?
- ajss 10y ago(defn [o n] (.write o n)) will have to look up the .write method by doing reflection on o & n at runtime, which is really slow. (defn [^WriteInterface o ^long n] (.write o n)) can be much faster because the compiler knows the type of o & n.
- abc_lisper 10y agoI think you meant to say, (defn func[o n] (.write ^WriteInterface o ^long n)) This is needed only when calling Java methods, because Java allows overloading. Clojure only does arity-overloading, not type overloading, and hence doesn't need the type info.
- moomin 10y agoShort version: put :warn-on-reflection in your leiningen file, And then add type hints (google them) to reduce the warnings to zero. There's no reason the average Clojure project ever need be using reflection for dispatch.
- raspasov 10y agoCorrect, a lot of the average application-level code you see in the wild does not have to worry about this. If we're talking about things like load-balancers or databases or endpoints/functions that will be hit 1000s of times per second - then it's a diff. conversation.
- hkjgkjy 10y agoYou don't need to specify what type a function takes in Clojure, which is often nice. If you do though, the compiler can sometimes optimize, and make things much faster. Basically, the compiler does not have to * look up the type of the thing * run the action on it but can instead skip straight to step 2. If you somewhere in your Clojure program (or repl) run (set! warn-on-reflection true) it will print to the console when you are doing reflection. I think it's a good idea to put in the beginning of your core namespace - so that it is always on. Why not always wear the seatbelt when driving, you know?