Haskell:自定义类型的派生显示

发布于 2024-11-09 01:19:51 字数 995 浏览 0 评论 0原文

我有这样的类型定义:

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

我想将此类型打印到交互式 shell (GHCi) 中。应该打印的只是 String 字段。

我尝试了这个:

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

但我仍然不断收到

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)

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

感谢您的帮助!

编辑:

供将来参考,固定版本是:

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

instance Show Operace where
    show (Op _ str _) = 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 _ str _) = str

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伏妖词 2024-11-16 01:19:51

您所做的实例声明是正确的方法。您似乎忘记从原始 data 声明中删除错误的 deriving 子句。

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

instance Show Operace where
   show (Op op str inv) = show 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
凉墨 2024-11-16 01:19:51

您可以派生 Show,只需先导入 Text.Show.Functions 即可。

You can derive Show, just import Text.Show.Functions first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文