File: TryFrom.hs

package info (click to toggle)
haskell-witch 1.2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 252 kB
  • sloc: haskell: 3,144; makefile: 6
file content (26 lines) | stat: -rw-r--r-- 1,002 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{-# LANGUAGE MultiParamTypeClasses #-}

module Witch.TryFrom where

import qualified Witch.TryFromException as TryFromException

-- | This type class is for converting values from some @source@ type into
-- 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.
--
-- This type class is for conversions that can sometimes fail. If your
-- conversion always succeeds, consider implementing @From@ instead.
class TryFrom source target where
  -- | This method implements the conversion of a value between types. At call
  -- sites you may want to use @tryInto@ instead.
  --
  -- > -- Avoid this:
  -- > tryFrom (x :: s)
  -- >
  -- > -- Prefer this:
  -- > tryFrom @s
  --
  -- Consider using @maybeTryFrom@ or @eitherTryFrom@ to implement this
  -- method.
  tryFrom :: source -> Either (TryFromException.TryFromException source target) target