Note the second line in this GHCi session. What is it about the Latitude type that allows me to use a “bare” number as a value, instead of having to invoke a constructor? I would like to do something similar with some of my own types.
λ> :m + Data.Geo.GPX.Type.Latitude
λ> let t = 45 :: Latitude
λ> t
45.0
I’ve examined the source code for the Latitude type, but I had trouble figuring it out at first. Eventually I found the answer, so I thought I’d document it here. See my answer below.
According to the Haskell98 standard, numeric literals are actually calls to
fromIntegerandfromRational. This allows them to be converted to any type that implements those functions (fromInteger is in the Prelude.Num typeclass and fromRational is in the Prelude.Fractional typeclass).http://www.haskell.org/onlinereport/basic.html#numeric-literals