【发布时间】:2017-03-03 10:49:34
【问题描述】:
考虑以下:
class Test m a where
t :: Int -> m a
instance Test [] Int where
t i = [i]
instance Test Maybe Int where
t i | i == 0 = Nothing
| otherwise = Just i
main = do
print $ t (22 :: Int) --Error!
这将引发以下错误:
Ambiguous type variables ‘m0’, ‘a0’ arising from a use of ‘print’
prevents the constraint ‘(Show (m0 a0))’ from being solved.
这是因为编译器无法确定要使用的m a 实例。我该如何明确说明这一点?
【问题讨论】: