尝试返回,而不是IO(前一个) [英] try is returning Either instead of IO (Either ex a)

查看:173
本文介绍了尝试返回,而不是IO(前一个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  go bs = 
case try $ parseRequest读者bs安全
左前 - > exceptionHandler作者ex
右(请求,bs') - > do
sendResponse writer =<<<应用程序请求
go bs'

但是,当使用尝试

 无法匹配预期类型'IO(E0(Request,ByteString) )'
与实际类型'任一t0 t1'
在模式中:左ex
在一种情况下:左ex - > exceptionHandler作者ex
在表达式中:
case try $ parseRequest reader bs secure of {
Left ex - > exceptionHandler作者ex
对(请求,bs')
- > do {sendResponse writer =<<应用请求;
go bs'}}

IO(E0(请求,ByteString))正是我从尝试而得到的,因为它的类型是 try :: Exception e => ; IO a - > IO(Ea),而是我得到 ea



什么是我缺少?

解决方案

try 确实产生一个 IO(EEA)。您收到错误消息,因为您将 try 生成的值与 Left ex 具有类型

>要修复代码,您需要从 IO (使用<$ c $)中获取 c>>> = < - do ),然后模式匹配。


I'm trying to handle exceptions from my request parser:

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

But I have an issue when using 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)) is exactly what I except to get from try because its type is try :: Exception e => IO a -> IO (Either e a), but instead I get Either e a.

What am I missing?

解决方案

try does produce an IO (Either e a). You're getting the error message because you're matching the value produced by try against the pattern Left ex, which has the type Either a b, not IO a.

To fix your code, you'd need to get the Either out of the IO (using >>= or <- inside do) and then pattern match against that.

这篇关于尝试返回,而不是IO(前一个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆