如何使用hFileSize获取Haskell中的文件大小

如何使用hFileSize获取Haskell中的文件大小,haskell,Haskell,我正试图获得真实世界Haskell建议的文件大小: getFileSize :: FilePath -> IO (Maybe Integer) getFileSize path = handle (\_ -> return Nothing) $ bracket (openFile path ReadMode) (hClose) (\h -> do size <- hFileSize h

我正试图获得真实世界Haskell建议的文件大小:

getFileSize :: FilePath -> IO (Maybe Integer)
getFileSize path = handle (\_ -> return Nothing)
                   $ bracket (openFile path ReadMode) (hClose) (\h -> do size <- hFileSize h
                                                                         return $ Just size)
getFileSize::FilePath->IO(可能是整数)
getFileSize path=handle(\ \ \->不返回任何内容)
$bracket(openFile路径读取模式)(hClose)(\h->do size不返回任何内容)
在表达式中:
句柄(\ \ \->不返回任何内容)
美元括号
(openFile路径读取模式)
(盐酸)
(\h

->do{size在我使用谷歌后,我解决了如下问题:

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
getFileSize::FilePath->IO(可能是整数)
getFileSize path=句柄处理程序
$bracket(openFile path ReadMode)(hClose)(\h->do size IO(可能是整数)
handler=不返回任何内容

请注意,这是因为现实世界中的Haskell是在
控件之前编写的。Exception
base
版本4中进行了修改。(旧接口已弃用,但在
控件中仍然可用。OldException
)。您可以通过启用
ScopedTypeVariables
-
(\(SomeException)->不返回任何内容)
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