简体   繁体   English

以下 haskell 类型类实例有什么问题?

[英]What is wrong with the following haskell typeclass instance?

data II = I Int Int deriving (Show)
instance II Show where
  show I a b = show (a+b)

showt.hs:3:2: show' is not a (visible) method of class II' showt.hs:3:2: show' is not a (visible) method of class

The class name should come before the type in the instance declaration. class 名称应位于实例声明中的类型之前。 You also need to remove the deriving clause, since you're providing your own instance instead of using one that's automatically derived.您还需要删除deriving子句,因为您提供的是自己的实例,而不是使用自动派生的实例。 You also need to add parenthesis around the single argument to show , otherwise it looks like 3 arguments to the parser.您还需要在show的单个参数周围添加括号,否则解析器看起来像 3 arguments。

data II = I Int Int
instance Show II where
    show (I a b) = show (a+b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM