OverloadedStrings不太合适

时间:2013-12-26 19:25:24

标签: haskell

此代码提供以下错误。我该如何解决?

230     let pipeline = [ ["$match" =: matchSelect],
231                         ["$group" =: ["_id" =: empty, "sum" =:
232                             ["$sum" =: "$score"]]] ]

Add.hs:232:40:
    No instance for (Data.String.IsString v0)
      arising from the literal `"$score"'
    The type variable `v0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Data.String.IsString Data.Text.Internal.Text
        -- Defined in `Data.Text'
      instance Data.String.IsString [Char] -- Defined in `Data.String'
    In the second argument of `(=:)', namely `"$score"'
    In the expression: "$sum" =: "$score"
    In the second argument of `(=:)', namely `["$sum" =: "$score"]'
Failed, modules loaded: Utils, Delete, Get, Migrate, Review, Validate.

1 个答案:

答案 0 :(得分:8)

(=:)具有多态类型,其中在结果类型中未提及参数中提到的类型变量,因此编译器没有足够的数量来决定表达式{中的第二个参数的类型{1}}。

字符串文字"$sum" =: "$score"给出约束"$score",而IsString v0给出约束(=:),但实际上并没有说“它必须是这种类型”,并且因为最终结果是Val v0类型,但未提及Field,使用最终结果的方式无法约束v

最简单的方法是给v一个类型签名,例如: "$score"或您真正想要的任何其他类型。