Skip to content

Instantly share code, notes, and snippets.

@passy
Last active August 29, 2015 14:01
Show Gist options
  • Save passy/41d4abf1dcb97e302ddb to your computer and use it in GitHub Desktop.
Save passy/41d4abf1dcb97e302ddb to your computer and use it in GitHub Desktop.
oh dear
{-# LANGUAGE DataKinds #-}
data ErrorCatA = WTFError String | OMGError String
data ErrorCatB = BadError Int | TerribleError Float
data GenericError = CatA ErrorCatA | CatB ErrorCatB
class GError a where
wrap :: a -> GenericError
instance GError ErrorCatA where
wrap = CatA
instance GError ErrorCatB where
wrap = CatB
handleError :: GenericError -> IO ()
handleError err =
putStrLn $ case err of
CatA (WTFError s) -> "WTF?! " ++ s
CatA (OMGError s) -> "OMG?! " ++ s
CatB (BadError i) -> "Bad! " ++ show i
CatB (TerribleError f) -> "Terrible! " ++ show f
main :: IO ()
main = do
let err0 = CatA $ WTFError "Sky's falling."
let err1 = CatB $ TerribleError 1.337
handleError err0
handleError err1
handleError $ wrap $ WTFError "Yo"
handleError $ wrap $ OMGError "this can't be right ..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment