Cabbage.hs:Tại sao biến loại này không rõ ràng?
module Cabbage where
class Cabbage a
where foo :: a -> String -- the parameter is only present for its type,
-- the parameter value will be ignored
bar :: String -> a
quux :: Cabbage a => String -> a
quux s = bar (s ++ foo (undefined :: a))
Khi tôi biên dịch (với GHC) tôi nhận được thông báo lỗi này:
Cabbage.hs:7:19:
Ambiguous type variable `a' in the constraint:
`Cabbage a' arising from a use of `foo' at Cabbage.hs:7:19-38
Probable fix: add a type signature that fixes these type variable(s)
Tôi không hiểu tại sao a
là mơ hồ. Chắc chắn a
trong dòng 7 giống với a
trong dòng 6? Làm thế nào để sửa lỗi này?
Ngoài ra, có cách nào tốt hơn để khai báo hằng số cho từng trường hợp không?