我不認爲這是一個錯誤,但我有點困惑,爲什麼這是行不通的。一個額外的問題是爲什麼它提到變量e?沒有變量e。不明確的類型變量錯誤msg
Prelude> :m +Control.Exception Prelude Control.Exception> handle (\_-> return "err") undefined <interactive>:1:0: Ambiguous type variable `e' in the constraint: `Exception e' arising from a use of `handle' at <interactive>:1:0-35 Probable fix: add a type signature that fixes these type variable(s) Prelude Control.Exception>
顯然它工作正常ghci 6.8,我使用6.10.1。
編輯:我已經最小化了代碼。我希望有兩個6.8相同的結果和6.10
class C a
foo :: C a => (a -> Int)-> Int
foo _ = 1
arg :: C a => a -> Int
arg _ = 2
bar :: Int
bar = foo arg
試圖編譯:
[1 of 1] Compiling Main (/tmp/foo.hs, interpreted) /tmp/foo.hs:12:10: Ambiguous type variable `a' in the constraint: `C a' arising from a use of `arg' at /tmp/foo.hs:12:10-12 Probable fix: add a type signature that fixes these type variable(s) Failed, modules loaded: none. Prelude Control.Exception>
爲什麼編譯器還需要知道哪些異常?該函數處理來自該類的每種類型。 – luntain 2009-01-10 21:40:56
由於「手柄」的類型;任何對`handle`的使用都必須應用於Exception類中的一個*特殊類型。由於您的處理程序適用於類中的所有類型,因此編譯器無法將類型分配給「句柄」。 ('e`來自'handle`類型。) – 2009-01-11 01:25:28