尝试返回Either而不是IO(前一个)

时间:2013-05-24 18:09:55

标签: haskell exception-handling

我正在尝试处理来自请求解析器的异常:

   go bs =                                                                 
       case try $ parseRequest reader bs secure of                             
         Left  ex             -> exceptionHandler writer ex                 
         Right (request, bs') -> do                                         
            sendResponse writer =<< app request                             
            go bs'                                                               

但使用try时出现问题:

Couldn't match expected type `IO (Either e0 (Request, ByteString))'
            with actual type `Either t0 t1'
In the pattern: Left ex
In a case alternative: Left ex -> exceptionHandler writer ex
In the expression:
  case try $ parseRequest reader bs secure of {
    Left ex -> exceptionHandler writer ex
    Right (request, bs')
      -> do { sendResponse writer =<< app request;
              go bs' } }

IO (Either e0 (Request, ByteString))正是我从try获取的内容,因为其类型为try :: Exception e => IO a -> IO (Either e a),而是Either e a

我错过了什么?

2 个答案:

答案 0 :(得分:7)

try会产生IO (Either e a)。您收到错误消息是因为您将try生成的值与模式Left ex匹配,后者的类型为Either a b,而不是IO a

要修复您的代码,您需要从Either中取出IO(使用>>=<-内的do,然后模式匹配。

答案 1 :(得分:5)

在GHC 7.6及更高版本中,您可以使用

try (parseRequest reader bs secure) >>= \case
    Left ex -> ...
    Right (request, bs') -> ...

如果您启用{-# LANGUAGE LambdaCase #-}