Skip to content

Policy on Show instances #155

@dmcclean

Description

@dmcclean

I recently learned that Show instances are supposed to generate valid Haskell code.

Ours doesn't, it just does human-readable pretty printing.

We could probably change it so that show (36 :: Double *~ inch) was something like 0.9144 *~ meter (with appropriate precedence) instead of 0.9144 m, is this desirable?

Relatedly, are we OK with overlapping show instances if we can prove that they give the same behavior in the overlapping case? This would be a boon to the fixed-point branch:

instance (KnownDimension d, Show a, Fractional a) => Show (Quantity d a) where
  -- we just show the value and add the unit, we know the scale factor is one

instance (KnownDimension d, KnownExactPi s, Show a, Real a) => Show (SQuantity s d a) where
  -- explained below

In the latter instance, we do the same thing as the first one if the scale factor isExactOne (thus guaranteeing the same behavior in the overlapping cases). If the scale factor is other than one, we convert something like (Quantity 372) :: SQuantity (1 / 10) DLength Int16 to something like "Quantity 372 {- 37.2 m -}" which (a) is valid Haskell, (b) round-trips the value, and (c) gives you some kind of clue to the meaning when you are in GHCi, which can get confusing with more complicated scale factors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions