Haskell:派生显示自定义类型 [英] Haskell: Deriving Show for custom type

查看:158
本文介绍了Haskell:派生显示自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  data Operace = Op(Int-> Int-> Int)String( Int-> Int-> Int)导出显示

我想将此类型打印到交互式壳(GHCi)。所有应该打印的都是字符串字段。



我试过了这个:

  instance显示Operace其中
显示(Op op str inv)= show str

但是我仍然继续收到$($) > Int - > Int))
从数据类型声明的deriving子句中产生
可能的修正:
添加一个实例声明(Show(Int - > Int- > Int))
或使用独立的派生实例声明,
,因此您可以自己指定实例上下文
当为(Show Operace)派生实例

我不想为 Show >(Int-> Int-> Int),我要打印的只是字符串。



感谢您的帮助! p>

编辑

为了将来的参考,固定版本是:

 数据Operace = Op(Int - > Int-> Int)String(Int-> Int-> Int)

实例显示Operace其中
显示(Op op str inv)= str


解决方案

您所做的实例声明是正确的方法。看起来你忘记从原始的 data 声明中删除派生子句。

 数据Operace = Op(Int-> Int-> Int)String(Int-> Int-> Int)

实例Show Operace其中
显示(Op op str inv)= show str


I have this type definition:

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

I want to print this type into the interactive shell (GHCi). All that should be printed is the String field.

I tried this:

instance Show Operace where
    show (Op op str inv) = show str

But I still keep getting

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)

I don't want to add Show for (Int->Int->Int), all I want to print is the string.

Thanks for help!

EDIT:

For future reference, the fixed version is:

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

instance Show Operace where
    show (Op op str inv) = str

解决方案

The instance declaration you made is the correct way to go. It seems you forgot to remove that faulty deriving clause from the original data declaration.

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

instance Show Operace where
   show (Op op str inv) = show str

这篇关于Haskell:派生显示自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆