0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Control.Monad.Error.Class

0
Posted at

初めてまともに使った。エラー抽象化できるのは気分が良い。

% runghc
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Monad.Error.Class

safeHead :: (Error e, MonadError e m) => [a] -> m a
safeHead (x:_) = return x
safeHead [] = throwError $ strMsg "NG"

main :: IO ()
main = let try = do
             (n :: Int) <- safeHead [3 ..]
             putStrLn $ show n
             (m :: Int) <- safeHead []
             putStrLn $ show m
             return ()
       in try `catchError` (\e -> print e)

【実行結果】
3
user error (NG)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?