17

Even looking closely over documentation on Clojure, I do not see any direct confirmation as to whether or not Clojure supports operator overloading.

If it does, could someone provide me with a quick snipplet of how to overload, let's say, the "+" operator to delegate to some predefined method that we can call myPlus.

I am very new to Clojure, so someone's help here would be greatly appreciated.

2 Answers 2

26

Clojure's (as any Lisp's) operators are plain functions; you can define an "operator" like a function:

(defn ** [x y] (Math/pow x y))

The "+" operator (and some other math-operators) is a special case in Clojure, since it is inlined (for the binary case, at least). You can to a certain extent avoid this by not referring to clojure.core (or excluding clojure.core/+) in your namespace, but this can be very hairy.

To create a namespace where + is redefined:

(ns my-ns
  (:refer-clojure :exclude [+]))

(defn + [x y] (println x y))

(+ "look" "ma")

One good strategy would probably be to make your + a multimethod and call core's + function for the numeric cases.

6
  • So, if I were to exclude clojure.core/+, I could do the following (defn + [x y] (myPlus x y)) correct? If so, that is exactly what I want to do. How do I exclude clojure.core/+ from my namespace? Oct 8, 2009 at 2:08
  • 1
    I've added this to my answer.
    – pmf
    Oct 8, 2009 at 2:14
  • Actually, after trying this: it doesn't seem to work. I try it and get the following error: java.lang.Exception: Name conflict, can't def + because namespace: my-ns refers to:#'clojure.core/+ (NO_SOURCE_FILE:5) Oct 8, 2009 at 6:39
  • Please enable public editing so I can fix my vote on your answer (and give you a well-deserved +1) Oct 8, 2009 at 6:46
  • I don't actually know how to do this.
    – pmf
    Oct 8, 2009 at 15:22
5

Take a look at this: http://clojure.org/multimethods

Certain functions, like + are core and cannot be redefined.

You could make a new function and call it ".+" or "!+" for example, which is similar in terms of readability.

Using the information in the multimethods URL included above, you can build a function that tells your .+ which implementation to use.

2
  • You didn't answer his question one way or the other.
    – chollida
    Oct 8, 2009 at 1:51
  • + can be easily redefined in a different namespace. In fact you can even redefine + in clojure.core if you like (though not recommended....)
    – mikera
    Jan 2, 2013 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.