Eq macro which matches anything

Hi guys, I'm doing Write yourself Scheme tutorial. And for testing it would be cool to have some kind of macro. -- My data type with Eq derived data LispError = NumArgs Integer [LispVal] | TypeMismatch String LispVal | Parser ParseError | BadSpecialForm String LispVal | NotFunction String String | UnboundVar String String | Default String deriving (Eq) -- I can call == NumArgs 1 [] == NumArgs 1 [] -- but sometimes I want to do (I know I can implement my own Eq, but I want to use both forms) NumArgs 1 [] == NumArgs 1 _ where I omit second parameter. I think derived Eq where it matches all the args is fine I just need to do macro which will get whateverType by compiler and always return True in Equality check. Do you think it is possible to do? I use Mockito library in java but java is OO, so completely different beast. Thank you

On 5 December 2015 at 09:02, frantisek kocun
Hi guys,
I'm doing Write yourself Scheme tutorial. And for testing it would be cool to have some kind of macro.
-- My data type with Eq derived data LispError = NumArgs Integer [LispVal] | TypeMismatch String LispVal | Parser ParseError | BadSpecialForm String LispVal | NotFunction String String | UnboundVar String String | Default String deriving (Eq)
-- I can call == NumArgs 1 [] == NumArgs 1 []
-- but sometimes I want to do (I know I can implement my own Eq, but I want to use both forms) NumArgs 1 [] == NumArgs 1 _
where I omit second parameter. I think derived Eq where it matches all the args is fine I just need to do macro which will get whateverType by compiler and always return True in Equality check. Do you think it is possible to do? I use Mockito library in java but java is OO, so completely different beast.
Thank you
You could always define a new function: eqError :: LispError -> LispError -> Bool eqError (NumArgs n1 _) (NumArgs n2 _) = n1 == n2 eqError TypeMisMatch{} TypeMismatch{} = True -- etc.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com

On Fri, Dec 4, 2015 at 2:02 PM, frantisek kocun
-- (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.
participants (3)
-
Bryan Richter
-
frantisek kocun
-
Ivan Lazar Miljenovic