使用 Haskell 的类型类,我创建了一个列表类型类的实例,其元素不是该类型类的实例:
class Three a where
three :: a -> [a]
instance (Three a) => Three [a] where
three x = [x, x, x]
对于整数列表和字符列表,我收到不同的错误消息:
字符
*Main> three ['c']
<interactive>:11:1:
No instance for (Three Char)
arising from a use of `three'
Possible fix: add an instance declaration for (Three Char)
In the expression: three ['c']
In an equation for `it': it = three ['c']
整数
*主要>三个[1, 2]
<interactive>:12:8:
Ambiguous type variable `t0' in the constraints:
(Num t0) arising from the literal `1' at <interactive>:12:8
(Three t0) arising from a use of `three' at <interactive>:12:1-5
Probable fix: add a type signature that fixes these type variable(s)
In the expression: 1
In the first argument of `three', namely `[1, 2]'
In the expression: three [1, 2]
为什么??
(现在,Chars 的错误已被理解 - 没有实例。但 Integers 的歧义错误对我来说并不清楚。我认为这可能不是因为整数已经是某物(Num)的实例,所以我创建了另一个任意类型类和它的一个实例用于 Char,但对于 Char 得到与以前相同的错误)。
我会感谢你的帮助