相关疑难解决方法(0)

多态模式匹配导致类型不明确

(灵感来自,无法在具有约束条件的多态元组上进行匹配,并基于我对自己答案的后续评论。)

考虑下面的最小示例:

test :: (Show a, Show b) => (a -> String, b -> String)
test = (show,show)

(resX, resY) = test
Run Code Online (Sandbox Code Playgroud)

这将导致以下错误:

    • Ambiguous type variable ‘a0’ arising from a use of ‘test’
      prevents the constraint ‘(Show a0)’ from being solved.
      Relevant bindings include
        resX :: a0 -> String (bound at so.hs:25:2)
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show Ordering -- Defined in ‘GHC.Show’ …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell pattern-matching

9
推荐指数
2
解决办法
139
查看次数

标签 统计

haskell ×1

pattern-matching ×1

polymorphism ×1