1
考慮如下:顯式指定類方法的返回類型?
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
的方式。我怎樣才能明確說明這一點?