如何使用catchIOError作为catch?

时间:2017-04-23 03:53:33

标签: haskell exception try-catch

我在源代码中找到了定义。

catchIOError :: IO a -> (IOError -> IO a) -> IO a
catchIOError = catch

如果我想使用catch代替catchIOError并获得相同的结果,我该怎么办?

catchIOError (readFile "test") (\_ -> return "")

并使用它:

catch (readFile "test") (\_ -> return "")

关于变量类型

会抛出异常
Ambiguous type variable ‘e0’ arising from a use of ‘catch’
prevents the constraint ‘(Exception e0)’ from being solved.
Probable fix: use a type annotation to specify what ‘e0’ should be.

换句话说,有没有办法在不定义新函数的情况下限制类型。

1 个答案:

答案 0 :(得分:3)

使用类型注释:

(catch :: IO a -> (IOError -> IO a) -> IO a) (readFile "test") (\_ -> return "")