我正在玩一些在线示例,其中一个有这样的短语:
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)
所以我想知道为什么我需要做出这个改变,如果这是最简洁的方法.(是的,我知道我可以使用替代形式的尝试,处理,...)