मैं आरडब्ल्यूएच पढ़ रहा हूँ, और मैं अध्याय 9. यह कोड का निम्न भाग का परिचय पर आए हैं:"संभाल" समारोह और असली दुनिया हास्केल
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;
.... }
क्या गलत यहाँ चल रहा है? यह संकलन क्यों नहीं करेगा?
हास्केल वेबसाइट पर 'handle' समारोह के लिए दस्तावेज़ इस बारे में बहुत स्पष्ट नहीं है (कम से कम प्रवेश स्तर के लोगों के लिए - जो प्रलेखन की आवश्यकता है) https://wiki.haskell.org/Exception बहुत स्पष्ट स्पष्टीकरण के लिए धन्यवाद कि कंपाइलर को हमें संभालने के लिए अपवाद प्रकार निर्दिष्ट करने की आवश्यकता है! – jocull