有点好奇为什么在下面需要实例声明,因为已经提供了默认实现。
module Example where
class Eq a => EQ a where
eq :: a -> a -> Bool
eq = (==)
-- why is this instance declaration needed?
instance EQ Int where
eq = (==)
f :: Int -> Int -> Bool
f = eq
如果我省略实例声明,就会出现错误。我确定这在Haskell报告中有解释,但要找到它需要一些时间。
class
实例不会自动地构造一个instance
。 - Willem Van Onseminstance
声明中不需要编写where
子句。只需编写instance EQ Int
即可,它将为Int
配备默认的eq
实现。 - Marc van Dongen