本文介绍了如何使用hFileSize在Haskell中获取文件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取Real World Haskell建议的文件大小:
I'm trying to get size of file like Real World Haskell recommends:
getFileSize :: FilePath -> IO (Maybe Integer)
getFileSize path = handle (\_ -> return Nothing)
$ bracket (openFile path ReadMode) (hClose) (\h -> do size <- hFileSize h
return $ Just size)
我收到此错误:
Ambiguous type variable `e0' in the constraint:
(GHC.Exception.Exception e0) arising from a use of `handle'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: handle (\ _ -> return Nothing)
In the expression:
handle (\ _ -> return Nothing)
$ bracket
(openFile path ReadMode)
(hClose)
(\ h
-> do { size <- hFileSize h;
return $ Just size })
In an equation for `getFileSize':
getFileSize path
= handle (\ _ -> return Nothing)
$ bracket
(openFile path ReadMode)
(hClose)
(\ h
-> do { size <- hFileSize h;
return $ Just size })
但是我不知道发生了什么事.
But I can't figure out what is going on.
推荐答案
我去Google之后,我解决了这样的问题:
After I went google I solved the problem like this:
getFileSize :: FilePath -> IO (Maybe Integer)
getFileSize path = handle handler
$ bracket (openFile path ReadMode) (hClose) (\h -> do size <- hFileSize h
return $ Just size)
where
handler :: SomeException -> IO (Maybe Integer)
handler _ = return Nothing
这篇关于如何使用hFileSize在Haskell中获取文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!