Newtypes in Time Libraries

Simon Marlow simonmar at microsoft.com
Mon Feb 21 06:33:18 EST 2005


On 18 February 2005 23:44, Ashley Yakeley wrote:

> 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?

I like to just provide instance Integral and document that the type is
an integral number of picoseconds.  It means you can use overloaded
constants, and the usual arithmetic operations on DiffTime.

Cheers,
	Simon


More information about the Libraries mailing list