如何写“显示"?数据的类型类 [英] How to write "Show" Typeclass for data
本文介绍了如何写“显示"?数据的类型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的data
,并且我想使用Show
Typeclass为其提供良好的输出.
I have a very simple data
and I want to use Show
Typeclass to give it a nice output.
data Fruit = Apple | Orange Int
instance Show Fruit
where
show Apple = "Hey Apple"
show Orange a = "X number of Orange"
这会出现以下错误,但我不知道如何解决:
This gives the following error but I don't know how to fix it:
Equations for `show' have different numbers of arguments
推荐答案
您只是忘记了一些括号:)
You just forgot some parens :)
instance Show Fruit where
show Apple = "Hey Apple"
show (Orange a) = show a ++ " number of Orange"
像Orange a
这样的模式需要经过parens才能与Apple a
之类的东西区分开来,而我们实际上有两个参数.
Patterns like Orange a
need parens to disambiguate from something like Apple a
where we really have two arguments.
这篇关于如何写“显示"?数据的类型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文