【发布时间】:2014-04-03 17:21:26
【问题描述】:
如果函数的返回是class ClassA,是否可以在这样的函数中返回ClassA 的任何实例?例如:someFunction :: (ClassA a) => String -> a
那么,为什么下面这个功能不起作用呢?注意String 是Eq 的一个实例
getAnyEq :: (Eq a) => String -> a
getAnyEq input |input == "1" = "something"
|otherwise = "other"
出现的错误是:
Could not deduce (a ~ [Char])
from the context (Eq a)
bound by the type signature for getAnyEq :: Eq a => String -> a
at src/InterceptorRegistry.hs:11:13-33
`a' is a rigid type variable bound by
the type signature for getAnyEq :: Eq a => String -> a
at src/InterceptorRegistry.hs:11:13
我试图在 Internet 资源上找到这个确切的解释,但我没有找到...你能给我看一些吗?
【问题讨论】:
-
A 类型
Eq a => a意味着 所有 我们所知道的a是它是Eq类的一个实例。在这种情况下,我们知道的更多(特别是String类型)。 -
事实上它是重复的,但这些新答案帮助我理解了为什么
getAnyEq函数不起作用。