To me, g /: f[g[x_]] := h[x]
is just verbose equivalent of f[g[x_]] := h[x]
. Can you raise an example that you have to use /:
?
Rcollyer has already given an excellent answer but here is an example of when you might use UpValues
: when you are defining a particular data structure, with its own Head
, and you want to defining how built in operations like arithmetic work with that structure. I once did this for a timeSeries
data structure where, for example, addition would match up the dates in the first columns and add the corresponding pairs of values in the second columns. Adding T * 2 vectors with dates in the first column would give nonsense dates if you hadn't defined such an operation using an UpValue
.