Подтвердить что ты не робот

Получить размер файла в Haskell

Я пытаюсь получить размер файла, например, Real World Haskell рекомендует:

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 })

Но я не могу понять, что происходит.

4b9b3361

Ответ 1

После того, как я пошел google, я решил проблему следующим образом:

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