我有一个函数(在模块中)返回 IO (Maybe a)
,其中 a 是 Serialize
的一个实例。
在我的主程序中,我这样调用它:
msg <- fun token
print msg
得到错误
Ambiguous type variable `a0' in the constraints:
(Data.Serialize.Serialize a0) arising from a use of `foo'
at test_00.hs:13:15-19
(Show a0) arising from a use of `print' at test_00.hs:17:9-13
Probable fix: add a type signature that fixes these type variable(s)
我确切地知道问题出在哪里,我可以使用 -XScopedTypeVariables 和我调用库函数的方式的一些更改来修复它,如下所示:
(msg :: Maybe String) <- cwPop token
print msg
但是,我宁愿避免使用 ScopedTypeVariables 并想知道我可以在哪里测试 msg 是否是 show 类的成员然后打印它。如果不做其他事情。
最佳答案
您可以为 <-
右侧的表达式提供类型签名没有扩展名,
msg <- fun token :: IO (Maybe String)
print msg
(我做了缩进以便 print
和 msg
不再是 fun
的参数,你的缩进似乎被打破了)。
关于haskell - 不使用 -XScopedTypeVariables 进行重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11261625/