[Haskell-cafe] generic way to construct and deconstruct a newtype

Henning Thielemann lemming at henning-thielemann.de
Tue Mar 1 11:09:35 UTC 2022


On Tue, 1 Mar 2022, PICCA Frederic-Emmanuel wrote:

> Hello, I try to write some code use to parse an ini file
> for one of my softwares, using config-ini[1].
>
> In this config file I have scientific values with units, meter, angstrom, degrees, etc...
> So I created a bunch of newtype in order to deal with this part of the config file
>
> newtype Meter = Meter (Length Double)
>  deriving (Eq, Show)
>
> newType Angstrom = Angstrom (Length Double)
>
> [...]

I would define
    newtype Quantity dimension a = Quantity a

or use one of the libraries for physical dimensions. Then I would convert 
all values to SI standard units, i.e. all lengths to meter. If you must 
preserve the units from the config file, I would instead define

    newtype Quantity dimension unit a = Quantity a


More information about the Haskell-Cafe mailing list