带有catch的Haskell类型错误

 000猫000故事 发布于 2023-02-13 17:25

我正在玩一些在线示例,其中一个有这样的短语:

   do ... contents <- getDirectoryContents path `catch` const (return []) 

但它不会为我编译,并给出错误:

No instance for (Exception e0) arising from a use of `catch'
The type variable `e0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
  instance Exception NestedAtomically
    -- Defined in `Control.Exception.Base'
  instance Exception NoMethodError
    -- Defined in `Control.Exception.Base'
  instance Exception NonTermination
    -- Defined in `Control.Exception.Base'
  ...plus 7 others
In a stmt of a 'do' block:
  contents <- getDirectoryContents path `catch` const (return [])
In the expression:
  do { contents <- getDirectoryContents path
                   `catch` const (return []);

我不得不改变它以给出处理程序的类型,现在可以,但有点麻烦:

contents <- getDirectoryContents path `catch` (\(SomeException e) -> const (return []) e)

所以我想知道为什么我需要做出这个改变,如果这是最简洁的方法.(是的,我知道我可以使用替代形式的尝试,处理,...)

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有