我有一个函数(在一个模块中)返回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类的成员然后打印它我可以测试的任何方式。如果不做别的事。
答案 0 :(得分:6)
您可以为<-
右侧的表达式提供类型签名,不带扩展名,
msg <- fun token :: IO (Maybe String)
print msg
(我做了缩进,以便print
和msg
不再是fun
的参数,你的缩进似乎被打破了。
答案 1 :(得分:1)
另一种可能性:
msg <- fun token
print (msg :: Maybe String)
(这里没有动态类型,尽管它看起来像是什么样子 - 我们只需要获取类型检查器的更多信息,风格如何)