Say I want to define a custom graphical representation for my function foo
.
I can do this using MakeBoxes
as in the following:
foo /: MakeBoxes[foo[x_, y_], StandardForm] := ToBoxes @ Graphics[{
Point@{x, y}
}, Frame -> True, PlotRange -> {{-2, 2}, {-2, 2}}
]
so that evaluating foo[1, 1.5]
produces a Graphics
object accordingly.
This also works consistently if another function foobar
is defined to act on a foo
object, like in
foobar[foo[x_, y_]] := (Print["Matched"]; x + y)
There is another way to achieve the same result, which is using Interpretation
.
I could define foo2
as
foo2[x_, y_] := Interpretation[
Graphics[{
Point@{x, y}
}, Frame -> True, PlotRange -> {{-2, 2}, {-2, 2}}
],
foo2[x, y]
]
This produces the same result, and works consistently when defining another function like
foobar2[foo2[x_, y_]] := (Print["Matched"]; x + y)
I (vaguely) know that Interpretation
produces an InterpretationBox
, so I'm guessing there should be fundamental differences between this and the result of using MakeBoxes
, but I don't know what they are exactly.
What are the circumstances in which one function should be preferred with respect to the other? And what the intrinsic differences?