我在源代码中找到了定义。
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.
换句话说,有没有办法在不定义新函数的情况下限制类型。
答案 0 :(得分:3)
使用类型注释:
(catch :: IO a -> (IOError -> IO a) -> IO a) (readFile "test") (\_ -> return "")