convertible-text-0.4.0.2: Typeclasses and instances for converting between types (deprecated)

Portability | portable
Stability | provisional
Maintainer | Michael Snoyman <michael@snoyman.com>

Data.Convertible.Utils

Description

Synopsis

- boundedConversion :: (Ord a, Bounded b, Show a, Show b, ConvertAttempt a Integer, ConvertAttempt b Integer, Typeable a, Typeable b) => (a -> Attempt b) -> a -> Attempt b
- data ConvertBoundsException v a = ConvertBoundsException v v a
- mkTypeName :: String -> TypeRep
- convertAttemptVia :: (ConvertAttempt a b, ConvertAttempt b c) => b -> a -> Attempt c
- convertSuccessVia :: (ConvertSuccess a b, ConvertSuccess b c) => b -> a -> c

# Documentation

boundedConversion

Arguments

:: (Ord a, Bounded b, Show a, Show b, ConvertAttempt a Integer, ConvertAttempt b Integer, Typeable a, Typeable b) |
=> (a -> Attempt b) | Function to do the conversion
-> a | Input data
-> Attempt b | Result

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.

data ConvertBoundsException v a

Constructors

ConvertBoundsException v v a |

Instances

Typeable2 ConvertBoundsException |
(Show v, Show a) => Show (ConvertBoundsException v a) |
(Show v, Show a, Typeable v, Typeable a) => Exception (ConvertBoundsException v a) |

mkTypeName :: String -> TypeRep

Useful for defining `Typeable` instances. Example:

    instance Typeable TimeOfDay where
        typeOf _ = mkTypeName "TimeOfDay"

convertAttemptVia

Arguments

:: (ConvertAttempt a b, ConvertAttempt b c) |
=> b | Dummy data to establish intermediate type - can be undefined
-> a | Input value
-> Attempt c | Result

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.

convertSuccessVia

Arguments

:: (ConvertSuccess a b, ConvertSuccess b c) |
=> b | Dummy data to establish intermediate type - can be undefined
-> a | Input value
-> c | Result

Same as `convertAttemptVia` for `ConvertSuccess`