[Haskell-cafe] Eq macro which matches anything

Bryan Richter b at chreekat.net
Sat Dec 5 18:49:58 UTC 2015


On Fri, Dec 4, 2015 at 2:02 PM, frantisek kocun <frantisek.kocun at gmail.com>
wrote:

> --  (I know I can implement my own Eq, but I want to use both forms)


The two computations are very different. Since the type of a value
determines which "Eq" computation to run, it is not advisable to try to get
two different computations to work for the same type.

What you can do instead is use a newtype:

-- | A variant that ignores the LispVal in NumArgs for Eq
newtype LispErrorI = LispErrorI LispError

instance Eq LispErrorI where
    (LispErrorI (NumArgs n _)) == (LispErrorI (NumArgs n' _)) = n == n'
    (LispErrorI e) == (LispErrorI e') = e == e'

Anytime you want to compare two LispError:s using the second computation,
you can wrap / unwrap the values in the newtype. It creates some extra
syntax noise, but it makes the intent very clear, and there's no runtime
cost.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151205/50ad25c9/attachment.html>


More information about the Haskell-Cafe mailing list