convertible-1.0.9: Typeclasses and instances for converting between typesSource codeContentsIndex
Data.Convertible.Utils
Portabilityportable
Stabilityprovisional
MaintainerJohn Goerzen <jgoerzen@complete.org>
Description
Synopsis
boundedConversion :: (Ord a, Bounded b, Show a, Show b, Convertible a Integer, Convertible b Integer, Typeable a, Typeable b) => (a -> ConvertResult b) -> a -> ConvertResult b
mkTypeName :: String -> TypeRep
convertVia :: (Convertible a b, Convertible b c) => b -> a -> ConvertResult c
Documentation
boundedConversionSource
:: (Ord a, Bounded b, Show a, Show b, Convertible a Integer, Convertible b Integer, Typeable a, Typeable b)
=> a -> ConvertResult bInput data
-> aResult
-> ConvertResult b

Utility function to perform bounds checking as part of a conversion.

Does this be examining the bounds of the destination type, converting to the type of the source via safeConvert, comparing to the source value. Results in an error if the conversion is out of bounds.

mkTypeName :: String -> TypeRepSource

Useful for defining Typeable instances. Example:

instance Typeable TimeOfDay where
    typeOf _ = mkTypeName "TimeOfDay"
convertViaSource
:: (Convertible a b, Convertible b c)
=> bInput value
-> aResult
-> ConvertResult c

Useful for defining conversions that are implemented in terms of other conversions via an intermediary type. Instead of:

instance Convertible CalendarTime POSIXTime where
    safeConvert a = do r <- safeConvert a
                       safeConvert (r :: ClockTime)

we can now write:

instance Convertible CalendarTime POSIXTime where
    safeConvert = convertVia (undefined::ClockTime)

which does the same thing -- converts a CalendarTime to a ClockTime, then a ClockTime to a POSIXTime, both using existing Convertible instances.

Produced by Haddock version 2.6.0