witch/src/lib/Witch/TryFrom.hs

21 lines
913 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
--
-- This type class is for conversions that can fail. If your conversion cannot
2021-05-11 02:22:57 +03:00
-- fail, 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-11 04:56:28 +03:00
-- sites you will usually want to use @tryInto@ instead of this method.
--
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