What is the correct way to override a method like \"+\"? Right now I have
(defn- + [x y] (replacement x y))
but this results in warnings on the
Although I don't recommend overriding core functions like +, you could use binding or let for this, it depends on what behavior you want:
(let [+ -] (reduce + [1 2 3])) ; -4
(defn my-plus [x] (reduce + x))
(let [+ -] (my-plus [1 2 3])) ;6
(binding [+ -] (my-plus [1 2 3])); -4
Like it has been said in the comments below, binding doesn't work this way anymore since clojure 1.3, since the var should be dynamic and +,-, etc aren't.
For testing purposes/mocking you could however get similar behaviour. In that case look at with-redefs (since clojure 1.3): http://clojuredocs.org/clojure_core/clojure.core/with-redefs
Also see: Let vs. Binding in Clojure