This is a sticky issue on which I hope to get some people's thoughts.
Currently diagrams-core (in the Graphics.Rendering.Diagrams.Transform
module) contains this code:
-- | Scale uniformly in every dimension by the given scalar.
scale :: (Transformable t, Fractional (Scalar (V t)), Eq (Scalar (V t)))
=> Scalar (V t) -> t -> t
scale 0 = error "scale by zero! Halp!" -- XXX what should be done here?
scale s = transform $ scaling s
However, I think it's really unacceptable for diagrams-core to be able
to crash like this. In the past I just thought, "well, it's not too
bad... why would anyone ever scale by zero?". However, yesterday
mgsloan and I were playing with his diagrams-ghci project [1] and we
had something like
foo # scale 20
and wanted to edit the scaling factor... and we happened to delete the
'2' first, but since it's live-updating it immediately sent 'foo #
scale 0' to the backend and the whole application promptly crashed.
The point is there are any number of legitimate or accidental reasons
this kind of thing could happen and the core library simply should not
crash, ever.
So, how to fix this? Possible solutions include:
* scale 0 is the identity function
* add a Monoid t constraint to scale, and set scale 0 = mempty
* add an 'empty' method to the Transformable class, and set scale 0
= empty
I can see pros and cons to each of these solutions, but instead of
saying what I think, I'd like to hear what others think -- and
especially if you have any other ideas for solving this issue.
-Brent
[1]
https://github.com/mgsloan/diagrams-ghci