首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Haskell错误:Exception.catch中的“类型变量不明确”;需要类型签名,但在哪里?

Haskell错误:Exception.catch中的“类型变量不明确”;需要类型签名,但在哪里?
EN

Stack Overflow用户
提问于 2011-04-02 23:57:17
回答 1查看 1.6K关注 0票数 2

我正在尝试将我的程序的错误处理转换为使用异常。我构建了一个REPL来控制我的模拟,在那里我还处理错误:

代码语言:javascript
运行
复制
> repl :: Sim -> IO Sim
> repl old_sim = do
>     new_sim <- E.catch (do line <- getLine
>                            commands <- parseCommand line
>                            runCommands commands old_sim)
>                        (\err -> do putStrLn . show $ err
>                                    return old_sim)
>     if alive new_sim
>        then repl new_sim
>        else return new_sim

parseCommands或runCommands都可以返回fail String,这两个函数都返回IO Sim

当我加载这个文件时,我得到了这个巨大的错误:

代码语言:javascript
运行
复制
Prelude> :l simple_sim
[1 of 1] Compiling Main             ( simple_sim.lhs, interpreted )

simple_sim.lhs:95:18:
    Ambiguous type variable `a0' in the constraints:
      (E.Exception a0) arising from a use of `E.catch'
                       at simple_sim.lhs:95:18-24
      (Show a0) arising from a use of `show' at simple_sim.lhs:100:49-52
    Probable fix: add a type signature that fixes these type variable(s)
    In a stmt of a 'do' expression:
        new_sim <- E.catch
                     (do { line <- getLine;
                           commands <- parseCommand line;
                           runCommands commands old_sim })
                     (\ err
                        -> do { putStrLn . show $ err;
                            return old_sim })
    In the expression:
      do { new_sim <- E.catch
                        (do { line <- getLine;
                              commands <- parseCommand line;
                              runCommands commands old_sim })
                        (\ err
                           -> do { putStrLn . show $ err;
                               .... });
           if alive new_sim then repl new_sim else return new_sim }
    In an equation for `repl':
        repl old_sim
          = do { new_sim <- E.catch
                              (do { line <- getLine;
                                    commands <- parseCommand line;
                                    .... })
                              (\ err -> do { ... });
                 if alive new_sim then repl new_sim else return new_sim }
Failed, modules loaded: none.

具体地说:

代码语言:javascript
运行
复制
Probable fix: add a type signature that fixes these type variable(s)

我应该在哪里插入这个类型签名?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-03 00:40:24

您需要在出错时使用它来确定要捕获的异常。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5524201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档