2021-04-10 20:34:26 +03:00
|
|
|
{-# 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.
|
2021-04-10 20:34:26 +03:00
|
|
|
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.
|
2021-04-10 20:34:26 +03:00
|
|
|
tryCast :: source -> Either (TryCastException.TryCastException source target) target
|