So I want to use java.awt.Color
for something, and I'd like to be able to write code like this:
(use 'java.awt.Color)
(= Color/BLUE (- Color/WHITE Color/RED Color/GREEN))
Looking at the core implementation of -
, it talks specifically about clojure.lang.Numbers
, which to me implies that there is nothing I do to 'hook' into the core implementation and extend it.
Looking around on the Internet, there seems to be two different things people do:
Write their own
defn -
function, which only knows about the data type they're interested in. To use you'd probably end up prefixing a namespace, so something like:(= Color/BLUE (scdf.color/- Color/WHITE Color/RED Color/GREEN))
Or alternatively
use
ing the namespace and useclojure.core/-
when you want number math.Code a special case into your
-
implementation that passes through toclojure.core/-
when your implementation is passed aNumber
.
Unfortunately, I don't like either of these. The first is probably the cleanest, as the second makes the presumption that the only things you care about doing maths on is their new datatype and numbers.
I'm new to Clojure, but shouldn't we be able to use Protocols or Multimethods here, so that when people create / use custom types they can 'extend' these functions so they work seemlessly? Is there a reason that +
,-
etc doesn't support this? (or do they? They don't seem to from my reading of the code, but maybe I'm reading it wrong).
If I want to write my own extensions to common existing functions such as +
for other datatypes, how should I do it so it plays nicely with existing functions and potentially other datatypes?
core/+
it doesn't use one, so there is nothing to hook into. Check out mikera's answer though, and follow his code, you'll see how you can do it with protocols. – Telemark