So, is it reasonable to derive Eq
instead of a self-made case-insensitive instance?
That sort of depends on what you want. I'm sure that the author of the package has their reasons for that Eq
instance.
Also, if i were to use this library, how could i provide my own Eq
instance for EmailAdress
?
You can't "override" their instance. The usual solution to this sort of problem is a newtype
wrapper on which you write your own instances:
newtype MyEmailAddress = MyEmailAddress EmailAddress
And then you are free to define your own version of equality, possibly as:
import Data.Char (toLower)
import qualified Data.ByteString.Char8 as DBC (map)
instance Eq MyEmailAddress where
MyEmailAddress (EmailAddress a1 d1) == MyEmailAddress (EmailAddress a2 d2)
= DBC.map toLower a1 == DBC.map toLower a2 && DBC.map toLower d1 == DBC.map toLower d2
While I'm at it, allow me to mention you can even define a pattern synonym which makes everything much nicer:
{-# LANGUAGE PatternSynonyms #-}
pattern Email address domain = MyEmailAddress (EmailAddress address domain)
Then, you can easily make one of your emails with Email "yourName" "yourDomain"
as well as pattern match on that. The Eq
instance looks a lot nicer with this:
instance Eq MyEmailAddress where
Email a1 d1 == Email a2 d2
= DBC.map toLower a1 == DBC.map toLower a2 && DBC.map toLower d1 == DBC.map toLower d2