使用(可能是)参数“通用”进行功能

时间:2019-09-06 09:11:20

标签: haskell type-inference

let updateFunc = updatedMaybeProperty srcTitle targetTitle :: (Title -> Maybe a) -> Maybe a
    _ = updateFunc (titleVersion      :: Title -> Maybe Text)
    _ = updateFunc (titleYearProduced :: Title -> Maybe Integer)

我在第3行中收到此错误:

    • Couldn't match type ‘Text’ with ‘Integer’
      Expected type: Title -> Maybe Text
        Actual type: Title -> Maybe Integer
    • In the first argument of ‘updateFunc’, namely
        ‘(titleYearProduced :: Title -> Maybe Integer)’

很显然,在第2行中,编译器会推断Maybe a的类型,并确定a必须始终为Text

如何防止这种情况并使updateFunc成为“泛型”,以便它与a的不同类型兼容?

1 个答案:

答案 0 :(得分:6)

尝试注释绑定,而不是表达式。

let updateFunc :: (Title -> Maybe a) -> Maybe a
    updateFunc = updatedMaybeProperty srcTitle targetTitle
    _ = updateFunc (titleVersion      :: Title -> Maybe Text)
    _ = updateFunc (titleYearProduced :: Title -> Maybe Integer)