witch/src/lib/Witch/TryCast.hs

19 lines
839 B
Haskell
Raw Normal View History

{-# LANGUAGE MultiParamTypeClasses #-}
module Witch.TryCast where
import qualified Witch.TryCastException as TryCastException
2021-04-18 21:01:05 +03:00
-- | This type class is for converting values from some @source@ type into
-- some other @target@ type. The constraint @TryCast 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.
--
-- This type class is for conversions that can fail. If your conversion cannot
-- fail, consider implementing @Cast@ instead.
class TryCast 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.
tryCast :: source -> Either (TryCastException.TryCastException source target) target