重构没有-XScopedTypeVariables

J F*_*sch 2 haskell

我有一个函数(在一个模块中)返回IO (Maybe a)a的实例Serialize.

从我的主程序我这称之为:

 msg <- fun token
 print msg 
Run Code Online (Sandbox Code Playgroud)

并得到错误

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)
Run Code Online (Sandbox Code Playgroud)

我确切地知道问题是什么,我可以使用-XScopedTypeVariables来修复它,以及我如何调用我的库函数的一些更改,如下所示:

(msg :: Maybe String) <- cwPop token
print msg
Run Code Online (Sandbox Code Playgroud)

但是,我宁愿避免使用ScopedTypeVariables,并想知道如果msg是show类的成员然后打印它我可以测试的任何方式.如果不做别的事.

Dan*_*her 6

您可以为<-没有扩展名右侧的表达式提供类型签名,

msg <- fun token :: IO (Maybe String)
print msg
Run Code Online (Sandbox Code Playgroud)

(我所做的压痕,这样printmsg不能参数fun了,你的缩进似乎被打破).