witch/source/library/Witch/TryFrom.hs

27 lines
1002 B
Haskell
Raw Normal View History

{-# LANGUAGE MultiParamTypeClasses #-}
2021-05-11 02:31:18 +03:00
module Witch.TryFrom where
import qualified Witch.TryFromException as TryFromException
2021-04-18 21:01:05 +03:00
-- | This type class is for converting values from some @source@ type into
2021-05-11 04:56:28 +03:00
-- some other @target@ type. The constraint @'TryFrom' source target@ means
-- that you may be able to convert from a value of type @source@ into a value
-- of type @target@, but that conversion may fail at runtime.
2021-04-18 21:01:05 +03:00
--
2021-05-28 23:51:26 +03:00
-- This type class is for conversions that can sometimes fail. If your
-- conversion always succeeds, consider implementing @From@ instead.
2021-05-11 02:31:18 +03:00
class TryFrom source target where
2021-04-18 21:01:05 +03:00
-- | This method implements the conversion of a value between types. At call
2021-05-28 23:51:26 +03:00
-- sites you may want to use @tryInto@ instead.
--
-- > -- Avoid this:
-- > tryFrom (x :: s)
-- >
-- > -- Prefer this:
-- > tryFrom @s
--
2021-05-11 04:04:51 +03:00
-- Consider using @maybeTryFrom@ or @eitherTryFrom@ to implement this
-- method.
tryFrom :: source -> Either (TryFromException.TryFromException source target) target