Newtypes in Time Libraries

Ashley Yakeley ashley at
Fri Feb 18 18:44:24 EST 2005

What sort of functions should be provided to access certain newtypes? 
For instance, consider this:

  newtype DiffTime = MkDiffTime Integer -- picoseconds

It's considered good practice to hide MkDiffTime. At the same time, I 
need to provide access to the underlying value, so I provide these:

  timeToSISeconds :: (Real a) => DiffTime -> a

  siSecondsToTime :: (Real a) => a -> DiffTime

These convert with picoseconds by multiplication or division. But do I 
also want to provide access to the picoseconds directly?

  timeToSIPicoseconds :: DiffTime -> Integer
  timeToSIPicoseconds (MkDiffTime ps) = ps

  siPicosecondsToTime :: Integer -> DiffTime
  siPicosecondsToTime = MkDiffTime

This makes certain other calculations in other modules easier.

I have a similar issue with TimeZones. My intention is to encode them as 
a count of minutes, hide the constructor, but provided minutesToTimeZone 
and timeZoneToMinutes. Is this sensible?

Ashley Yakeley, Seattle WA

More information about the Libraries mailing list