Haskell:为自定义类型导出显示

Mat*_*ský 30 haskell show typeclass ghci

我有这种类型的定义:

data Operace = Op (Int->Int->Int) String (Int->Int->Int) deriving Show
Run Code Online (Sandbox Code Playgroud)

我想将此类型打印到交互式shell(GHCi)中.所有应该打印的是该String领域.

我试过这个:

instance Show Operace where
    show (Op op str inv) = show str
Run Code Online (Sandbox Code Playgroud)

但我仍然坚持下去

No instance for (Show (Int -> Int -> Int))
  arising from the 'deriving' clause of a data type declaration
Possible fix:
  add an instance declaration for (Show (Int -> Int -> Int))
  or use a standalone 'deriving instance' declaration,
       so you can specify the instance context yourself
When deriving the instance for (Show Operace)
Run Code Online (Sandbox Code Playgroud)

我不希望添加Show(Int->Int->Int),所有我想打印的字符串.

感谢帮助!

编辑:

为了将来参考,修复版本是:

data Operace = Op (Int->Int->Int) String (Int->Int->Int)

instance Show Operace where
    show (Op _ str _) = str
Run Code Online (Sandbox Code Playgroud)

hug*_*omg 25

您所做的实例声明是正确的方法.您似乎忘记deriving从原始data声明中删除该错误条款.

data Operace = Op (Int->Int->Int) String (Int->Int->Int)

instance Show Operace where
   show (Op op str inv) = show str
Run Code Online (Sandbox Code Playgroud)


Tho*_*son 24

你可以派生Show,Text.Show.Functions先导入.