Haskell:自定义类型的派生显示
我有这样的类型定义:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所做的实例声明是正确的方法。您似乎忘记从原始
data
声明中删除错误的deriving
子句。The instance declaration you made is the correct way to go. It seems you forgot to remove that faulty
deriving
clause from the originaldata
declaration.您可以派生
Show
,只需先导入Text.Show.Functions
即可。You can derive
Show
, just importText.Show.Functions
first.