witch/src/lib/Witch/TryFrom.hs

22 lines
929 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 02:31:18 +03:00
-- some other @target@ type. The constraint @TryFrom source target@ means that
2021-04-18 21:01:05 +03:00
-- 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.
--
-- 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
-- sites you will usually want to use @tryFrom@ or @tryInto@ instead of this
-- method.
--
-- Consider using @maybeTryCast@ or @eitherTryCast@ to implement this
-- method.
tryFrom :: source -> Either (TryFromException.TryFromException source target) target