私はRWHを読んでいる、と私は第9章これは、次のコードを紹介しに来ている:「ハンドル」機能と実世界Haskellの
import System.IO
import Control.Exception
saferFileSize :: FilePath -> IO (Maybe Integer)
saferFileSize path = handle (\_ -> return Nothing) $ do
h <- openFile path ReadMode
size <- hFileSize h
hClose h
return (Just size)
それは与え、しかし、コンパイルされません次のエラーメッセージ:
test.hs:5:22:
Ambiguous type variable `e0' in the constraint:
(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)
$ do { h <- openFile path ReadMode;
size <- hFileSize h;
hClose h;
return (Just size) }
In an equation for `saferFileSize':
saferFileSize path
= handle (\ _ -> return Nothing)
$ do { h <- openFile path ReadMode;
size <- hFileSize h;
hClose h;
.... }
ここで何が問題になりますか?なぜそれはコンパイルされませんか?
Haskellのウェブサイト上で 'handle'機能のドキュメントには、このことについて非常に不明瞭である(少なくとも、エントリレベルの人々に! - ドキュメントを必要とするもの)https://wiki.haskell.org/Exception非常に明確な説明をお寄せいただきありがとうございます。コンパイラは処理する例外の種類を指定するだけです! – jocull