在尝试学习Haskell时继续出现障碍.
我正在关注"真实世界Haskell",当谈到让他们的一个复杂的例子工作时,我得到以下错误
" e' in the constraint:
在FoldDir.hs中使用`handle'产生的模糊类型变量GHC.Exception.Exception e':88:14-61可能的修复:添加修复这些类型变量的类型签名"
我的相关代码是:
import Control.Exception (bracket, handle)
maybeIO :: IO a -> IO (Maybe a)
maybeIO act = handle (\_ -> return Nothing) (Just `liftM` act)
Run Code Online (Sandbox Code Playgroud)
我该如何根除这个错误?
sep*_*p2k 14
您需要为handler-function的参数指定一个类型,因此它知道要处理哪种类型的异常.
您可以通过命名函数来执行此操作
import Control.Exception (handle, SomeException)
maybeIO act = handle handler (Just `liftM` act)
where handler :: SomeException -> IO (Maybe a)
handler _ = return Nothing
Run Code Online (Sandbox Code Playgroud)
或者使用ScopedTypeVariables
扩展名:
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Exception (handle, SomeException)
maybeIO act = handle (\(_ :: SomeException) -> return Nothing) (Just `liftM` act)
Run Code Online (Sandbox Code Playgroud)