2021-04-10 20:34:26 +03:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
|
2021-05-11 02:31:18 +03:00
|
|
|
module Witch.TryFrom where
|
2021-04-10 20:34:26 +03:00
|
|
|
|
2021-05-11 02:36:22 +03:00
|
|
|
import qualified Witch.TryFromException as TryFromException
|
2021-04-10 20:34:26 +03:00
|
|
|
|
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-01 22:09:44 +03:00
|
|
|
--
|
2021-05-11 04:04:51 +03:00
|
|
|
-- Consider using @maybeTryFrom@ or @eitherTryFrom@ to implement this
|
2021-05-01 22:09:44 +03:00
|
|
|
-- method.
|
2021-05-11 02:36:22 +03:00
|
|
|
tryFrom :: source -> Either (TryFromException.TryFromException source target) target
|