haskell - 不使用 -XScopedTypeVariables 进行重构

标签 haskell

我有一个函数(在模块中)返回 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

(我做了缩进以便 printmsg 不再是 fun 的参数,你的缩进似乎被打破了)。

关于haskell - 不使用 -XScopedTypeVariables 进行重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11261625/

相关文章:

string - Data.Text 的好处

haskell - 将类型变量约束为具体类型

opengl - haskell 与 Data.Map 更新有关的问题

haskell - 当提供多个互斥选项时,如何使用 optparse-applicative 提供特定的错误消息?

list - Haskell获取IO列表元素

Haskell parsec 解析一串项目

haskell - 提供 monad 堆栈的测试实现

haskell - 指定输入的列表类型

haskell - 将提升函数应用于 Haskell 中的元组(任意长度)

haskell - RankNTypes 和教堂数字