在这段代码中:
import System.Posix.Files
import Control.Exception
safeStat :: FilePath -> IO (Maybe FileStatus)
safeStat path =
handle (\_ -> return Nothing) (getFileStatus path >>= (return . Just))
我收到此错误(在 ghci 中):
Ambiguous type variable `e0' in the constraint:
(Exception e0) arising from a use of `handle'
...
我可以通过执行以下操作来消除错误:
nothing :: IOException -> Maybe a
nothing _ = Nothing
safeStat :: FilePath -> IO (Maybe FileStatus)
safeStat path =
handle (return . nothing) (getFileStatus path >>= (return . Just))
这是怎么回事???我希望处理程序处理任何异常。