雖然試圖編譯下面的代碼,讀取從安全包材上readMay其增強版本。「Ambigous型變量」錯誤定製定義當「讀出」功能
readI :: (Typeable a, Read a) => String -> a
readI str = case readMay str of
Just x -> x
Nothing -> error ("Prelude.read failed, expected type: " ++
(show (typeOf > (undefined :: a))) ++
"String was: " ++ str)
我得到一個錯誤從GHC:
WavefrontSimple.hs:54:81:
歧義類型變量'A '中的約束:
'分型一個'
從所產生在src/WavefrontSimple.hs中使用`typeOf':54:81-103
可能的修復:添加修復這些類型變量的類型簽名`
我不明白爲什麼。我應該修正什麼以達到我的意思?
編輯:好的,所以解決方案使用ScopedTypeVariables
和forall a
在簽名作品。但爲什麼以下產生與上述相似的錯誤?由於使用了asTypeOf :: a -> a -> a
,因此編譯器應推斷正確的類型。
readI :: (Typeable a, Read a) => String -> a
readI str = let xx = undefined in
case readMay str of
Just x -> x `asTypeOf` xx
Nothing -> error ("Prelude.read failed, expected type: "
++ (show (typeOf xx)) ++
"String was: " ++ str)
我投票在編輯之前投票結束。現在問題已經更新,我相信它不再是重複的,但不能刪除VTC。我想我只能等待它自己過期。 – ephemient 2010-03-25 22:26:52