data Set a = Set [a]
-- Example 1
instance (Eq a, Ord a) => Eq (Set a) where
(Set xs) == (Set ys) = (sort xs) == (sort ys)
-- Example 2
instance (Eq a, Ord a) => Eq (Set a) where
(==) = eqSet
eqSet (Set xs) (Set ys) = (sort xs) == (sort ys)
示例1和2是覆盖Set数据类型的相等性测试时覆盖“ ==”的不同方法。我想知道哪种方法更可取,为什么?
谢谢。
除了eqSet
标识符的存在和内联的关注点之外,没有其他区别可以用{-# INLINE eqSet #-}
实用程序解决。我个人的喜好是使用内联版本,因为单独的定义没有任何价值,但我认为这没有多大意义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句