| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Witch
Description
The Witch package is a library that allows you to confidently convert values between various types. This module exports everything you need to perform conversions or define your own. It is designed to be imported unqualified, so getting started is as easy as:
>>>import Witch
In typical usage, the functions that you will use most often are
into for conversions that always succeed and
tryInto for conversions that sometimes fail.
Please consider reading the blog post that announces this library: https://taylor.fausak.me/2021/07/13/witch/
Synopsis
- class From source target where
- from :: source -> target
- into :: forall target source. From source target => source -> target
- class TryFrom source target where
- tryFrom :: source -> Either (TryFromException source target) target
- tryInto :: forall target source. TryFrom source target => source -> Either (TryFromException source target) target
- data TryFromException source target = TryFromException source (Maybe SomeException)
- type Latin1S = ISO_8859_1 ByteString
- type Latin1L = ISO_8859_1 ByteString
- type ISO_8859_1 = Tagged "ISO-8859-1"
- type Utf8S = UTF_8 ByteString
- type Utf8L = UTF_8 ByteString
- type UTF_8 = Tagged "UTF-8"
- type Utf16LS = UTF_16LE ByteString
- type Utf16LL = UTF_16LE ByteString
- type UTF_16LE = Tagged "UTF-16LE"
- type Utf16BS = UTF_16BE ByteString
- type Utf16BL = UTF_16BE ByteString
- type UTF_16BE = Tagged "UTF-16BE"
- type Utf32LS = UTF_32LE ByteString
- type Utf32LL = UTF_32LE ByteString
- type UTF_32LE = Tagged "UTF-32LE"
- type Utf32BS = UTF_32BE ByteString
- type Utf32BL = UTF_32BE ByteString
- type UTF_32BE = Tagged "UTF-32BE"
- via :: forall through source target. (From source through, From through target) => source -> target
- tryVia :: forall through source target. (TryFrom source through, TryFrom through target) => source -> Either (TryFromException source target) target
- maybeTryFrom :: (source -> Maybe target) -> source -> Either (TryFromException source target) target
- eitherTryFrom :: Exception exception => (source -> Either exception target) -> source -> Either (TryFromException source target) target
- unsafeFrom :: (HasCallStack, TryFrom source target, Show source, Typeable source, Typeable target) => source -> target
- unsafeInto :: forall target source. (HasCallStack, TryFrom source target, Show source, Typeable source, Typeable target) => source -> target
- liftedFrom :: forall source target (m :: Type -> Type). (TryFrom source target, Lift target, Show source, Typeable source, Typeable target, Quote m) => source -> Code m target
- liftedInto :: forall target source (m :: Type -> Type). (TryFrom source target, Lift target, Show source, Typeable source, Typeable target, Quote m) => source -> Code m target
Type classes
From
class From source target where Source #
This type class is for converting values from some source type into
some other target type. The constraint means that
you can convert from a value of type From source targetsource into a value of type
target.
This type class is for conversions that always succeed. If your conversion
sometimes fails, consider implementing TryFrom instead.
Minimal complete definition
Nothing
Methods
from :: source -> target Source #
This method implements the conversion of a value between types. At call
sites you may prefer to use into instead.
-- Avoid this: from (x :: s) -- Prefer this (using [@TypeApplications@](https://downloads.haskell.org/ghc/9.6.1/docs/users_guide/exts/type_applications.html) language extension): from @s x
The default implementation of this method simply calls coerce,
which works for types that have the same runtime representation. This
means that for newtypes you do not need to implement this method at
all. For example:
>>>newtype Name = Name String>>>instance From Name String>>>instance From String Name
Instances
| From Pico DiffTime Source # | Uses |
Defined in Witch.Instances | |
| From Pico NominalDiffTime Source # | Uses |
Defined in Witch.Instances | |
| From ByteString ByteString Source # | Uses |
Defined in Witch.Instances | |
| From ByteString ShortByteString Source # | Uses |
Defined in Witch.Instances | |
| From ByteString ByteString Source # | Uses |
Defined in Witch.Instances | |
| From ShortByteString ByteString Source # | Uses |
Defined in Witch.Instances | |
| From Int16 Int32 Source # | Uses |
Defined in Witch.Instances | |
| From Int16 Int64 Source # | Uses |
Defined in Witch.Instances | |
| From Int16 Integer Source # | Uses |
Defined in Witch.Instances | |
| From Int16 Double Source # | Uses |
Defined in Witch.Instances | |
| From Int16 Float Source # | Uses |
Defined in Witch.Instances | |
| From Int16 Int Source # | Uses |
Defined in Witch.Instances | |
| From Int32 Int64 Source # | Uses |
Defined in Witch.Instances | |
| From Int32 Integer Source # | Uses |
Defined in Witch.Instances | |
| From Int32 Double Source # | Uses |
Defined in Witch.Instances | |
| From Int64 Integer Source # | Uses |
Defined in Witch.Instances | |
| From Int8 Int16 Source # | Uses |
Defined in Witch.Instances | |
| From Int8 Int32 Source # | Uses |
Defined in Witch.Instances | |
| From Int8 Int64 Source # | Uses |
Defined in Witch.Instances | |
| From Int8 Integer Source # | Uses |
Defined in Witch.Instances | |
| From Int8 Double Source # | Uses |
Defined in Witch.Instances | |
| From Int8 Float Source # | Uses |
Defined in Witch.Instances | |
| From Int8 Int Source # | Uses |
Defined in Witch.Instances | |
| From Rational UniversalTime Source # | Uses |
Defined in Witch.Instances | |
| From Rational TimeOfDay Source # | Uses |
Defined in Witch.Instances | |
| From Rational Double Source # | Uses |
Defined in Witch.Instances | |
| From Rational Float Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Int32 Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Int64 Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Word32 Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Word64 Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Integer Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Natural Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Double Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Float Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Int Source # | Uses |
Defined in Witch.Instances | |
| From Word16 Word Source # | Uses |
Defined in Witch.Instances | |
| From Word32 Int64 Source # | Uses |
Defined in Witch.Instances | |
| From Word32 Word64 Source # | Uses |
Defined in Witch.Instances | |
| From Word32 Integer Source # | Uses |
Defined in Witch.Instances | |
| From Word32 Natural Source # | Uses |
Defined in Witch.Instances | |
| From Word32 Double Source # | Uses |
Defined in Witch.Instances | |
| From Word64 Integer Source # | Uses |
Defined in Witch.Instances | |
| From Word64 Natural Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Int16 Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Int32 Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Int64 Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Word16 Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Word32 Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Word64 Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Integer Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Natural Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Double Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Float Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Int Source # | Uses |
Defined in Witch.Instances | |
| From Word8 Word Source # | Uses |
Defined in Witch.Instances | |
| From Text Text Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf16BL Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf16BS Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf16LL Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf16LS Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf32BL Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf32BS Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf32LL Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf32LS Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf8L Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf8S Source # | Uses |
Defined in Witch.Instances | |
| From Text String Source # | Uses |
Defined in Witch.Instances | |
| From Text Text Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf16BL Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf16BS Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf16LL Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf16LS Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf32BL Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf32BS Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf32LL Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf32LS Source # | Converts via |
Defined in Witch.Instances | |
| From Text Utf8L Source # | Uses |
Defined in Witch.Instances | |
| From Text Utf8S Source # | Converts via |
Defined in Witch.Instances | |
| From Text String Source # | Uses |
Defined in Witch.Instances | |
| From CalendarDiffDays CalendarDiffTime Source # | Uses |
Defined in Witch.Instances | |
| From Day DayOfWeek Source # | Uses |
Defined in Witch.Instances | |
| From Day Integer Source # | Uses |
Defined in Witch.Instances | |
| From DiffTime Pico Source # | Uses |
Defined in Witch.Instances | |
| From DiffTime TimeOfDay Source # | Uses |
Defined in Witch.Instances | |
| From NominalDiffTime Pico Source # | Uses |
Defined in Witch.Instances | |
| From NominalDiffTime CalendarDiffTime Source # | Uses |
Defined in Witch.Instances | |
| From POSIXTime UTCTime Source # | Uses |
Defined in Witch.Instances | |
| From SystemTime AbsoluteTime Source # | Uses |
Defined in Witch.Instances | |
| From SystemTime POSIXTime Source # | Uses |
Defined in Witch.Instances | |
| From SystemTime UTCTime Source # | Uses |
Defined in Witch.Instances | |
| From UTCTime POSIXTime Source # | Uses |
Defined in Witch.Instances | |
| From UTCTime SystemTime Source # | Uses |
Defined in Witch.Instances | |
| From UniversalTime Rational Source # | Uses |
Defined in Witch.Instances | |
| From TimeOfDay Rational Source # | Uses |
Defined in Witch.Instances | |
| From TimeOfDay DiffTime Source # | Uses |
Defined in Witch.Instances | |
| From ZonedTime UTCTime Source # | Uses |
Defined in Witch.Instances | |
| From Latin1L Text Source # | Converts via |
Defined in Witch.Instances | |
| From Latin1L Text Source # | Uses |
Defined in Witch.Instances | |
| From Latin1L String Source # | Converts via |
Defined in Witch.Instances | |
| From Latin1S Text Source # | Uses |
Defined in Witch.Instances | |
| From Latin1S Text Source # | Converts via |
Defined in Witch.Instances | |
| From Latin1S String Source # | Converts via |
Defined in Witch.Instances | |
| From Integer Day Source # | Uses |
Defined in Witch.Instances | |
| From Natural Integer Source # | Uses |
Defined in Witch.Instances | |
| From String Text Source # | Uses |
Defined in Witch.Instances | |
| From String Text Source # | Uses |
Defined in Witch.Instances | |
| From String Utf16BL Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf16BS Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf16LL Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf16LS Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf32BL Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf32BS Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf32LL Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf32LS Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf8L Source # | Converts via |
Defined in Witch.Instances | |
| From String Utf8S Source # | Converts via |
Defined in Witch.Instances | |
| From Double Float Source # | Uses |
Defined in Witch.Instances | |
| From Float Double Source # | Uses |
Defined in Witch.Instances | |
| From Int Int64 Source # | Uses |
Defined in Witch.Instances | |
| From Int Integer Source # | Uses |
Defined in Witch.Instances | |
| From Word Word64 Source # | Uses |
Defined in Witch.Instances | |
| From Word Integer Source # | Uses |
Defined in Witch.Instances | |
| From Word Natural Source # | Uses |
Defined in Witch.Instances | |
| From a a Source # | Uses |
Defined in Witch.Instances | |
| From ByteString [Word8] Source # | Uses |
Defined in Witch.Instances | |
| From ByteString [Word8] Source # | Uses |
Defined in Witch.Instances | |
| From ShortByteString [Word8] Source # | Uses |
Defined in Witch.Instances | |
| From IntSet [Int] Source # | Uses |
Defined in Witch.Instances | |
| Num a => From a (Complex a) Source # | Uses |
Defined in Witch.Instances | |
| Integral a => From a (Ratio a) Source # | Uses |
Defined in Witch.Instances | |
| (Generic s, Generic t, GFrom (Rep s) (Rep t)) => From s (Generically t) Source # | See the |
Defined in Witch.Generic | |
| HasResolution a => From Integer (Fixed a) Source # | Uses |
Defined in Witch.Instances | |
| From a (Tagged t a) Source # | Uses |
Defined in Witch.Instances | |
| From [Word8] ByteString Source # | Uses |
Defined in Witch.Instances | |
| From [Word8] ByteString Source # | Uses |
Defined in Witch.Instances | |
| From [Word8] ShortByteString Source # | Uses |
Defined in Witch.Instances | |
| From [Int] IntSet Source # | Uses |
Defined in Witch.Instances | |
| From (IntMap v) [(Int, v)] Source # | Uses |
Defined in Witch.Instances | |
| From (Seq a) [a] Source # | Uses |
Defined in Witch.Instances | |
| From (Set a) [a] Source # | Uses |
Defined in Witch.Instances | |
| From (NonEmpty a) [a] Source # | Uses |
Defined in Witch.Instances | |
| From [(Int, v)] (IntMap v) Source # | Uses |
Defined in Witch.Instances | |
| From [a] (Seq a) Source # | Uses |
Defined in Witch.Instances | |
| Ord a => From [a] (Set a) Source # | Uses |
Defined in Witch.Instances | |
| Ord k => From [(k, v)] (Map k v) Source # | Uses |
Defined in Witch.Instances | |
| HasResolution a => From (Fixed a) Rational Source # | Uses |
Defined in Witch.Instances | |
| From (Map k v) [(k, v)] Source # | Uses |
Defined in Witch.Instances | |
| From (TryFromException source oldTarget) (TryFromException source newTarget) Source # | Uses |
Defined in Witch.Instances Methods from :: TryFromException source oldTarget -> TryFromException source newTarget Source # | |
| From (Tagged t a) a Source # | Uses |
Defined in Witch.Instances | |
| From (Tagged t a) (Tagged u a) Source # | Uses |
into :: forall target source. From source target => source -> target Source #
This is the same as from except that the type variables are in the
opposite order.
-- Avoid this: from x :: t -- Prefer this: into @t x
TryFrom
class TryFrom source target where Source #
This type class is for converting values from some source type into
some other target type. The constraint means
that you may be able to convert from a value of type TryFrom source targetsource into a value
of type target, but that conversion may fail at runtime.
This type class is for conversions that can sometimes fail. If your
conversion always succeeds, consider implementing From instead.
Methods
tryFrom :: source -> Either (TryFromException source target) target Source #
This method implements the conversion of a value between types. At call
sites you may want to use tryInto instead.
-- Avoid this: tryFrom (x :: s) -- Prefer this: tryFrom @s
Consider using maybeTryFrom or eitherTryFrom to implement this
method.
Instances
| TryFrom Int16 Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int16 -> Either (TryFromException Int16 Int8) Int8 Source # | |
| TryFrom Int16 Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int16 -> Either (TryFromException Int16 Word16) Word16 Source # | |
| TryFrom Int16 Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int16 -> Either (TryFromException Int16 Word32) Word32 Source # | |
| TryFrom Int16 Word64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int16 -> Either (TryFromException Int16 Word64) Word64 Source # | |
| TryFrom Int16 Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int16 -> Either (TryFromException Int16 Word8) Word8 Source # | |
| TryFrom Int16 Natural Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int16 -> Either (TryFromException Int16 Natural) Natural Source # | |
| TryFrom Int16 Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int16 -> Either (TryFromException Int16 Word) Word Source # | |
| TryFrom Int32 Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Int16) Int16 Source # | |
| TryFrom Int32 Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Int8) Int8 Source # | |
| TryFrom Int32 Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Word16) Word16 Source # | |
| TryFrom Int32 Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Word32) Word32 Source # | |
| TryFrom Int32 Word64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Word64) Word64 Source # | |
| TryFrom Int32 Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Word8) Word8 Source # | |
| TryFrom Int32 Natural Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Natural) Natural Source # | |
| TryFrom Int32 Float Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Float) Float Source # | |
| TryFrom Int32 Int Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Int) Int Source # | |
| TryFrom Int32 Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int32 -> Either (TryFromException Int32 Word) Word Source # | |
| TryFrom Int64 Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Int16) Int16 Source # | |
| TryFrom Int64 Int32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Int32) Int32 Source # | |
| TryFrom Int64 Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Int8) Int8 Source # | |
| TryFrom Int64 Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Word16) Word16 Source # | |
| TryFrom Int64 Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Word32) Word32 Source # | |
| TryFrom Int64 Word64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Word64) Word64 Source # | |
| TryFrom Int64 Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Word8) Word8 Source # | |
| TryFrom Int64 Natural Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Natural) Natural Source # | |
| TryFrom Int64 Double Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Double) Double Source # | |
| TryFrom Int64 Float Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Float) Float Source # | |
| TryFrom Int64 Int Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Int) Int Source # | |
| TryFrom Int64 Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int64 -> Either (TryFromException Int64 Word) Word Source # | |
| TryFrom Int8 Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int8 -> Either (TryFromException Int8 Word16) Word16 Source # | |
| TryFrom Int8 Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int8 -> Either (TryFromException Int8 Word32) Word32 Source # | |
| TryFrom Int8 Word64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int8 -> Either (TryFromException Int8 Word64) Word64 Source # | |
| TryFrom Int8 Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int8 -> Either (TryFromException Int8 Word8) Word8 Source # | |
| TryFrom Int8 Natural Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int8 -> Either (TryFromException Int8 Natural) Natural Source # | |
| TryFrom Int8 Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int8 -> Either (TryFromException Int8 Word) Word Source # | |
| TryFrom Word16 Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word16 -> Either (TryFromException Word16 Int16) Int16 Source # | |
| TryFrom Word16 Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word16 -> Either (TryFromException Word16 Int8) Int8 Source # | |
| TryFrom Word16 Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word16 -> Either (TryFromException Word16 Word8) Word8 Source # | |
| TryFrom Word32 Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word32 -> Either (TryFromException Word32 Int16) Int16 Source # | |
| TryFrom Word32 Int32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word32 -> Either (TryFromException Word32 Int32) Int32 Source # | |
| TryFrom Word32 Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word32 -> Either (TryFromException Word32 Int8) Int8 Source # | |
| TryFrom Word32 Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word32 -> Either (TryFromException Word32 Word16) Word16 Source # | |
| TryFrom Word32 Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word32 -> Either (TryFromException Word32 Word8) Word8 Source # | |
| TryFrom Word32 Float Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word32 -> Either (TryFromException Word32 Float) Float Source # | |
| TryFrom Word32 Int Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word32 -> Either (TryFromException Word32 Int) Int Source # | |
| TryFrom Word32 Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word32 -> Either (TryFromException Word32 Word) Word Source # | |
| TryFrom Word64 Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Int16) Int16 Source # | |
| TryFrom Word64 Int32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Int32) Int32 Source # | |
| TryFrom Word64 Int64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Int64) Int64 Source # | |
| TryFrom Word64 Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Int8) Int8 Source # | |
| TryFrom Word64 Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Word16) Word16 Source # | |
| TryFrom Word64 Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Word32) Word32 Source # | |
| TryFrom Word64 Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Word8) Word8 Source # | |
| TryFrom Word64 Double Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Double) Double Source # | |
| TryFrom Word64 Float Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Float) Float Source # | |
| TryFrom Word64 Int Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Int) Int Source # | |
| TryFrom Word64 Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word64 -> Either (TryFromException Word64 Word) Word Source # | |
| TryFrom Word8 Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word8 -> Either (TryFromException Word8 Int8) Int8 Source # | |
| TryFrom Text Latin1L Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Text Latin1S Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Text Latin1L Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Text Latin1S Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf16BL Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf16BL Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf16BL String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf16BS Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf16BS Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf16BS String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf16LL Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf16LL Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf16LL String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf16LS Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf16LS Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf16LS String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf32BL Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf32BL Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf32BL String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf32BS Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf32BS Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf32BS String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf32LL Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf32LL Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf32LL String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf32LS Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf32LS Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf32LS String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf8L Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf8L Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf8L String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf8S Text Source # | Uses |
Defined in Witch.Instances | |
| TryFrom Utf8S Text Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Utf8S String Source # | Converts via |
Defined in Witch.Instances | |
| TryFrom Integer Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Int16) Int16 Source # | |
| TryFrom Integer Int32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Int32) Int32 Source # | |
| TryFrom Integer Int64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Int64) Int64 Source # | |
| TryFrom Integer Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Int8) Int8 Source # | |
| TryFrom Integer Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Word16) Word16 Source # | |
| TryFrom Integer Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Word32) Word32 Source # | |
| TryFrom Integer Word64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Word64) Word64 Source # | |
| TryFrom Integer Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Word8) Word8 Source # | |
| TryFrom Integer Natural Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Natural) Natural Source # | |
| TryFrom Integer Double Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Double) Double Source # | |
| TryFrom Integer Float Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Float) Float Source # | |
| TryFrom Integer Int Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Int) Int Source # | |
| TryFrom Integer Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Integer -> Either (TryFromException Integer Word) Word Source # | |
| TryFrom Natural Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Int16) Int16 Source # | |
| TryFrom Natural Int32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Int32) Int32 Source # | |
| TryFrom Natural Int64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Int64) Int64 Source # | |
| TryFrom Natural Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Int8) Int8 Source # | |
| TryFrom Natural Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Word16) Word16 Source # | |
| TryFrom Natural Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Word32) Word32 Source # | |
| TryFrom Natural Word64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Word64) Word64 Source # | |
| TryFrom Natural Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Word8) Word8 Source # | |
| TryFrom Natural Double Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Double) Double Source # | |
| TryFrom Natural Float Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Float) Float Source # | |
| TryFrom Natural Int Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Int) Int Source # | |
| TryFrom Natural Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Natural -> Either (TryFromException Natural Word) Word Source # | |
| TryFrom String Latin1L Source # | |
Defined in Witch.Instances | |
| TryFrom String Latin1S Source # | |
Defined in Witch.Instances | |
| TryFrom Double Int16 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Int16) Int16 Source # | |
| TryFrom Double Int32 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Int32) Int32 Source # | |
| TryFrom Double Int64 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Int64) Int64 Source # | |
| TryFrom Double Int8 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Int8) Int8 Source # | |
| TryFrom Double Rational Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Rational) Rational Source # | |
| TryFrom Double Word16 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Word16) Word16 Source # | |
| TryFrom Double Word32 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Word32) Word32 Source # | |
| TryFrom Double Word64 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Word64) Word64 Source # | |
| TryFrom Double Word8 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Word8) Word8 Source # | |
| TryFrom Double Integer Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Integer) Integer Source # | |
| TryFrom Double Natural Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Natural) Natural Source # | |
| TryFrom Double Int Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Int) Int Source # | |
| TryFrom Double Word Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Double -> Either (TryFromException Double Word) Word Source # | |
| TryFrom Float Int16 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Int16) Int16 Source # | |
| TryFrom Float Int32 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Int32) Int32 Source # | |
| TryFrom Float Int64 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Int64) Int64 Source # | |
| TryFrom Float Int8 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Int8) Int8 Source # | |
| TryFrom Float Rational Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Rational) Rational Source # | |
| TryFrom Float Word16 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Word16) Word16 Source # | |
| TryFrom Float Word32 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Word32) Word32 Source # | |
| TryFrom Float Word64 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Word64) Word64 Source # | |
| TryFrom Float Word8 Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Word8) Word8 Source # | |
| TryFrom Float Integer Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Integer) Integer Source # | |
| TryFrom Float Natural Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Natural) Natural Source # | |
| TryFrom Float Int Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Int) Int Source # | |
| TryFrom Float Word Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Float -> Either (TryFromException Float Word) Word Source # | |
| TryFrom Int Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Int16) Int16 Source # | |
| TryFrom Int Int32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Int32) Int32 Source # | |
| TryFrom Int Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Int8) Int8 Source # | |
| TryFrom Int Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Word16) Word16 Source # | |
| TryFrom Int Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Word32) Word32 Source # | |
| TryFrom Int Word64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Word64) Word64 Source # | |
| TryFrom Int Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Word8) Word8 Source # | |
| TryFrom Int Natural Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Natural) Natural Source # | |
| TryFrom Int Double Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Double) Double Source # | |
| TryFrom Int Float Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Float) Float Source # | |
| TryFrom Int Word Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Int -> Either (TryFromException Int Word) Word Source # | |
| TryFrom Word Int16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Int16) Int16 Source # | |
| TryFrom Word Int32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Int32) Int32 Source # | |
| TryFrom Word Int64 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Int64) Int64 Source # | |
| TryFrom Word Int8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Int8) Int8 Source # | |
| TryFrom Word Word16 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Word16) Word16 Source # | |
| TryFrom Word Word32 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Word32) Word32 Source # | |
| TryFrom Word Word8 Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Word8) Word8 Source # | |
| TryFrom Word Double Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Double) Double Source # | |
| TryFrom Word Float Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Float) Float Source # | |
| TryFrom Word Int Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Word -> Either (TryFromException Word Int) Int Source # | |
| HasResolution a => TryFrom Rational (Fixed a) Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Rational -> Either (TryFromException Rational (Fixed a)) (Fixed a) Source # | |
| (Eq a, Num a) => TryFrom (Complex a) a Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Complex a -> Either (TryFromException (Complex a) a) a Source # | |
| (Eq a, Num a) => TryFrom (Ratio a) a Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: Ratio a -> Either (TryFromException (Ratio a) a) a Source # | |
| TryFrom [a] (NonEmpty a) Source # | Uses |
Defined in Witch.Instances Methods tryFrom :: [a] -> Either (TryFromException [a] (NonEmpty a)) (NonEmpty a) Source # | |
| HasResolution a => TryFrom (Fixed a) Integer Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Fixed a -> Either (TryFromException (Fixed a) Integer) Integer Source # | |
| (HasResolution s, HasResolution t) => TryFrom (Fixed s) (Fixed t) Source # | Converts via |
Defined in Witch.Instances Methods tryFrom :: Fixed s -> Either (TryFromException (Fixed s) (Fixed t)) (Fixed t) Source # | |
tryInto :: forall target source. TryFrom source target => source -> Either (TryFromException source target) target Source #
This is the same as tryFrom except that the type variables are
in the opposite order.
-- Avoid this: tryFrom x :: Either (TryFromException s t) t -- Prefer this: tryInto @t x
Data types
data TryFromException source target Source #
This exception is thrown when a TryFrom conversion fails. It has the
original source value that caused the failure and it knows the target
type it was trying to convert into. It also has an optional
SomeException for communicating what went wrong while
converting.
Constructors
| TryFromException source (Maybe SomeException) |
Instances
| (Show source, Typeable source, Typeable target) => Exception (TryFromException source target) Source # | |
Defined in Witch.TryFromException Methods toException :: TryFromException source target -> SomeException fromException :: SomeException -> Maybe (TryFromException source target) displayException :: TryFromException source target -> String backtraceDesired :: TryFromException source target -> Bool | |
| (Show source, Typeable source, Typeable target) => Show (TryFromException source target) Source # | |
Defined in Witch.TryFromException Methods showsPrec :: Int -> TryFromException source target -> ShowS show :: TryFromException source target -> String showList :: [TryFromException source target] -> ShowS | |
| From (TryFromException source oldTarget) (TryFromException source newTarget) Source # | Uses |
Defined in Witch.Instances Methods from :: TryFromException source oldTarget -> TryFromException source newTarget Source # | |
Encodings
Witch supports decoding text and encoding byte strings using various
character encodings. For example to convert from a String into a
strict ByteString, you can use the
Utf8S alias:
into@ByteString(into@Utf8S("..." ::String)) -- "..."
And to convert from a strict ByteString into a
String, you can use the Utf8S alias in the other
direction:
tryInto@String(into@Utf8S("..." ::ByteString)) -- Right "..."
type Latin1S = ISO_8859_1 ByteString Source #
The ISO_8859_1 encoding for strict ByteStrings.
type Latin1L = ISO_8859_1 ByteString Source #
The ISO_8859_1 encoding for lazy ByteStrings.
type ISO_8859_1 = Tagged "ISO-8859-1" Source #
Utilities
via :: forall through source target. (From source through, From through target) => source -> target Source #
This function first converts from some source type into some through
type, and then converts that into some target type. Usually this is used
when writing From instances. Sometimes this can be used to work
around the lack of an instance that should probably exist.
-- Avoid this: from @u . into @u -- Prefer this: via @u
tryVia :: forall through source target. (TryFrom source through, TryFrom through target) => source -> Either (TryFromException source target) target Source #
This is similar to via except that it works with TryFrom
instances instead. This function is especially convenient because juggling
the types in the TryFromException can be tedious.
-- Avoid this:
case tryInto @u x of
Left (TryFromException _ e) -> Left $ TryFromException x e
Right y -> case tryFrom @u y of
Left (TryFromException _ e) -> Left $ TryFromException x e
Right z -> Right z
-- Prefer this:
tryVia @umaybeTryFrom :: (source -> Maybe target) -> source -> Either (TryFromException source target) target Source #
This function can be used to implement tryFrom with a function
that returns Maybe. For example:
-- Avoid this: tryFrom s = case f s of Nothing -> Left $ TryFromException s Nothing Just t -> Right t -- Prefer this: tryFrom = maybeTryFrom f
eitherTryFrom :: Exception exception => (source -> Either exception target) -> source -> Either (TryFromException source target) target Source #
This function can be used to implement tryFrom with a function
that returns Either. For example:
-- Avoid this: tryFrom s = case f s of Left e -> Left . TryFromException s . Just $ toException e Right t -> Right t -- Prefer this: tryFrom = eitherTryFrom f
Unsafe
These functions should only be used in two circumstances: When you know
a conversion is safe even though you can't prove it to the compiler, and
when you're alright with your program crashing if the conversion fails.
In all other cases you should prefer the normal conversion functions like
tryFrom. And if you're converting a literal value,
consider using the Template Haskell conversion functions like
liftedFrom.
unsafeFrom :: (HasCallStack, TryFrom source target, Show source, Typeable source, Typeable target) => source -> target Source #
This function is like tryFrom except that it will throw an
impure exception if the conversion fails.
-- Avoid this: either throw id . tryFrom @s -- Prefer this: unsafeFrom @s
unsafeInto :: forall target source. (HasCallStack, TryFrom source target, Show source, Typeable source, Typeable target) => source -> target Source #
This function is like tryInto except that it will throw an impure
exception if the conversion fails.
-- Avoid this: either throw id . tryInto @t -- Prefer this: unsafeInto @t
Template Haskell
This library uses typed Template Haskell, which may be a little
different than what you're used to. Normally Template Haskell uses the
$(...) syntax for splicing in things to run at compile time. The typed
variant uses the $$(...) syntax for splices, doubling up on the dollar
signs. Other than that, using typed Template Haskell should be pretty
much the same as using regular Template Haskell.
liftedFrom :: forall source target (m :: Type -> Type). (TryFrom source target, Lift target, Show source, Typeable source, Typeable target, Quote m) => source -> Code m target Source #
This is like unsafeFrom except that it works at compile time
rather than runtime.
-- Avoid this: unsafeFrom @s "some literal" -- Prefer this: $$(liftedFrom @s "some literal")
liftedInto :: forall target source (m :: Type -> Type). (TryFrom source target, Lift target, Show source, Typeable source, Typeable target, Quote m) => source -> Code m target Source #
This is like unsafeInto except that it works at compile time
rather than runtime.
-- Avoid this: unsafeInto @t "some literal" -- Prefer this: $$(liftedInto @t "some literal")
Notes
Motivation
Haskell provides many ways to convert between common types, and core
libraries add even more. It can be challenging to know which function to
use when converting from some source type a to some target type b. It
can be even harder to know if that conversion is safe or if there are any
pitfalls to watch out for.
This library tries to address that problem by providing a common
interface for converting between types. The From type class
is for conversions that cannot fail, and the TryFrom type
class is for conversions that can fail. These type classes are inspired
by the From
trait in Rust.
Type applications
Although you can use this library without the TypeApplications
language extension, the extension is strongly recommended. Since most
functions provided by this library are polymorphic in at least one type
variable, it's easy to use them in a situation that would be ambiguous.
Normally you could resolve the ambiguity with an explicit type signature,
but type applications are much more ergonomic. For example:
-- Avoid this: f . (from :: Int8 -> Int16) . g -- Prefer this: f . from @Int8 @Int16 . g
Most functions in this library have two versions with their type variables in opposite orders. That's because usually one side of the conversion or the other already has its type inferred by context. In those situations it makes sense to only provide one type argument.
-- Avoid this: (assuming f :: Int16 -> ...) f $ from @Int8 @Int16 0 -- Prefer this: f $ from @Int8 0
-- Avoid this: (assuming x :: Int8) g $ from @Int8 @Int16 x -- Prefer this: g $ into @Int16 x
Alternatives
Many Haskell libraries already provide similar functionality. How is this library different?
Coercible: This type class is solved by the compiler, but it only works for types that have the same runtime representation. This is very convenient fornewtypes, but it does not work for converting between arbitrary types likeInt8andInt16.Convertible: This popular conversion type class is similar to what this library provides. The main difference is that it does not differentiate between conversions that can fail and those that cannot.From: This type class is almost identical to what this library provides. Unfortunately it is part of thebasementpackage, which is an alternative standard library that some people may not want to depend on.Inj: This type class requires instances to be an injection, which means that no two input values should map to the same output. That restriction prohibits many useful instances. Also many instances throw impure exceptions.
In addition to those general-purpose type classes, there are many alternatives for more specific conversions. How does this library compare to those?
- Monomorphic conversion functions like
Data.Text.packare explicit but not necessarily convenient. It can be tedious to manage the imports necessary to use the functions. And if you want to put them in a custom prelude, you will have to come up with your own names. - Polymorphic conversion methods like
toEnumare more convenient but may have unwanted semantics or runtime behavior. For example theEnumtype class is more or less tied to theIntdata type and frequently throws impure exceptions. - Polymorphic conversion functions like
fromIntegralare very convenient. Unfortunately it can be challenging to know which types have the instances necessary to make the conversion possible. And even if the conversion is possible, is it safe? For example converting a negativeIntinto aWordwill overflow, which may be surprising.
Instances
When should you add a From (or TryFrom)
instance for some pair of types? This is a surprisingly tricky question
to answer precisely. Instances are driven more by guidelines than rules.
- Conversions must not throw impure exceptions. This means no
undefinedor anything equivalent to it. - Conversions should be unambiguous. If there are multiple reasonable
ways to convert from
atob, then you probably should not add aFrominstance for them. Conversions should be lossless. If you have
From a bthen no twoavalues should be converted to the samebvalue.- Some conversions necessarily lose information, like converting from a list into a set.
If you have both
From a bandFrom b a, thenfrom @b @a . from @a @bshould be the same asid. In other words,aandbare isomorphic.- This often true, but not always. For example, converting a list into a set will remove duplicates. And then converting back into a list will put the elements in ascending order.
If you have both
From a bandFrom b c, then you could also haveFrom a cand it should be the same asfrom @b @c . from @a @b. In other words,Fromis transitive.- This is not always true. For example an
Int8may be represented as a number in JSON, whereas anInt64might be represented as a string. That meansinto @JSON (into @Int64 int8)would not be the same asinto @JSON int8.
- This is not always true. For example an
- You should not have both a
Frominstance and aTryFrominstance for the same pair of types. - If you have a
FromorTryFrominstance for a pair of types, then you should probably have aFromorTryFrominstance for the same pair of types but in the opposite direction. In other words if you haveFrom a bthen you should haveFrom b aorTryFrom b a.
In general if s is a t, then you should add a From
instance for it. But if s merely can be a t, then you could add a
TryFrom instance for it. And if it is technically
possible to convert from s to t but there are a lot of caveats, you
probably should not write any instances at all.
Laws
As the previous section notes, there aren't any cut and dried laws for
the From and TryFrom type classes. However it can be useful to
consider the following equations for guiding instances:
-- same strictness seq (from @a @b x) y = seq x y seq (tryFrom @a @b x) y = seq x y
-- round trip from @b @a (from @a @b x) = x
-- transitive from @b @c (from @a @b x) = from @a @c x tryFrom @b @a (from @a @b x) = Right x if isRight (tryFrom @a @b x) then fmap (from @b @a) (tryFrom @a @b x) = Right x if isRight (tryFrom @a @b x) then do fmap (tryFrom @b @a) (tryFrom @a @b x) = Right (Right x)
Integral types
There are a lot of types that represent various different ranges of
integers, and Witch may not provide the instances you want. In particular
it does not provide a total way to convert from an Int32 into an Int.
Why is that?
The Haskell Language Report only demands that Ints have at least 30
bits of precision. That means a reasonable Haskell implementation could
have an Int type that's smaller than the Int32 type.
However in practice everyone uses the same Haskell implementation: GHC.
And with GHC the Int type always has 32 bits of precision, even on
32-bit architectures. So for almost everybody, it's probably safe to use
unsafeFrom @Int32 @Int. Similarly most software these days runs on
machines with 64-bit architectures. That means it's also probably safe
for you to use unsafeFrom @Int64 @Int.
All of the above also applies for Word, Word32, and Word64.
Downsides
As the author of this library, I obviously think that everyone should use it because it's the greatest thing since sliced bread. But nothing is perfect, so what are some downsides to this library?
- More specific type classes are often better. For example,
IsString sis more useful thatFrom String s. The former says that the typesis the same as a string literal, but the latter just says you can produce a value of typeswhen given a string. - The
Fromtype class works great for specific pairs of types, but can get confusing when it's polymorphic. For example if you have some function with aFrom s tconstraint, that doesn't really tell you anything about what it's doing.