虽然试图编译下面的代码,读取从安全包材上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